wrap text in php

php-sniplets 1 Comment »

How to force text to wrap after a certain number of characters

A function that almost developers find really convenient is word-wrap. If you’ve a long string of text that carries
no specific formatting, you will be able to use word-wrap to insert a character, specified newline character (\n),
at a defined interval. word-wrap attends not to separate words unless you specific say it to. This function may
be specially valuable once it touches on building well-laid-out e-mail messages.

To utilize word-wrap, we barely pass it a string. word-wrap’s default behaviour is to enclose the text as just
about 75 characters as conceivable (it will not break words), entering a newline character (\n) at all breakpoint.

In this case, we mean to output HTML, so we provide 2 additional arguments to modify this default behaviour:

<?php $string = “Here is a long sentence that will be abbreviate at seventy
. characters automatically. Do not care,
. no words will follow broken up.”; echo wordwrap($string, 70, “<br />”); ?>

On this call, word-wrap encloses the text at 70 characters, and insets <br /> tags rather than new line
characters. Hera what it turnouts:

This is a long sentence that will be cut at seventy characters<br />
automatically. Do not care, none word will follow broken up.

Therefore, we have wrangled this clumsy sentence into something long more controllable without breaking
some of the words.

simple mail form in php

php-sniplets 1 Comment »

Form to mail

A very simple form mail PHP script that shows a contact form to provide visitors to your web site to send you a message via e-mail. Built in security keeps spammers hijacking it from additional URL.

<?php

/**

* Change the e-mail address to your own.
*
* $empty_fields and $thankyou_messages can be modified
* if you like.
*/

// Modify to your own e-mail address

$your_email = “your@sitenasme.com”;

// This is what is showed in the e-mail subject line

// Modify it if you wish

$subject = “Message thru your contact form”;

// This is showed if all the fields are not fulfilled in

$empty_fields_message = “<p>Opps… get back and fill in all the form.</p>”;

// This is showed when the e-mail was sent

$thankyou_message = “<p>Thank’s  ! Your e-mail has been sent.</p>”;

// You don’t need to edit the code anymore.Thats it :) !!

$name = stripslashes($_POST['txtname']);

$email = stripslashes($_POST['txtemail']);

$message = stripslashes($_POST['txt.Message']);

if (!isset($_POST['txtName'])) {

?>

<form method=”post” action=”<?php echo $_SERVER['REQUEST_URI']; ?>”>

<p><label for=”txtName”>Name:</label><br />

<input type=”text” title=”Put your name here” name=”txtName” /></p>

<p><label for=”txtEmail”>e-mail:</label><br />

<input type=”text” title=”Put your e-mail address here” name=”txtEmail” /></p>

<p><label for=”txt.Message”>Put the message here:</label><br />

<textarea title=”Put your message here” name=”txtMessage”></textarea></p>

<p><label title=”Send “>

<input type=”submit” value=”Send” /></label></p>

</form>

<?php
}

elseif (empty($name) || empty($e-mail) || empty($messag)) {

echo $empty_fields_message;

}

else {

// Stop the form being exploited from an outside domain

// Get the referring URL

$referer = $_SERVER['HTTP_REFERER'];

// Get the domain name of this page

$this_url = “https://”.$_SERVER['HTTP_HOST'].$_SERVER["REQUEST_URI"];

// When the referring domain and the URL of this page do not check then

// Show a message and do not send the e-mail.

if ($referer != $this_url) {

echo ” You don’t have license to utilize this script from a different domain.”;

exit;

}

// The URLs checked then send the e-mail

mail($your_email, $subject, $message, “From: $name <$email>”);

// Show the thank you message

echo $thankyou_message;

}

?>

Handling Files in php

php-sniplets No Comments »

Handling Files

To apply the file functions, you just need to point them at the file they’ve to read, applying a path that is relative to the PHP script that runs the function. Even so, the absolute majority of PHP file functions use a somewhat another mechanism to get at a file–a mechanism that is really alike to that utilized to connect to a database. The process uses the fclose function to “disconnect and fopen function to “connect”. The value generated from the fopen function is a PHP file pointer, a.k.a. the handle of the file. When we get a handle on a file, we can apply it to execute an assortment of functionings on the file, admitting modifying it, reading to it, append it, etcetera.

Sample

This mere example shows how to close and open that “association” to the file:

<?php $location = ‘writeFileHandling.htm’; $fp = fopen($location, ‘rb’);  the file handle $fp is now usable fclose($fp); echo $file; ?>

Once you apply fopen to associate to a file, you must assign the route to the file and a manner in which the file is to be got at (specified r as read only). The b mode indicator shows that the file is to follow open in binary mode. The binary mode had better ever be assigned to ensure the movability of your code ‘tween OSs. For further information about the several modes that are accessible, read the manual here -> http://www.php.net/fopen/

Entries RSS Comments RSS Log in