Werbung

Werbung

Werbung

Archive

Folge uns auf Twitter

Werbung

Jede Menge neue Twitter Follower bekommen. Die Software hat einen kostenlosen Trial Mode, der bis zu 250 Follower bringt.

Willkommen

Willkommen im PHP Archiv Blog! Hier gibts Infos zum Thema PHP Codeschnipsel und Tipps. Bei unserem Projekt phparchiv.de, finden Sie über 5700 Scripte.

String

Email Adressen aus einem vorgegebenen String extrahieren

Diese Funktion extrahiert Email Adressen aus einem vorgegebenen String und gibt ein Array zurück.

function extract_emails($str){
    // This regular expression extracts all emails from
    // a string:
    $regexp = '/([a-z0-9_\.\-])+\@(([a-z0-9\-])+\.)+([a-z0-9]{2,4})+/i';
    preg_match_all($regexp, $str, $m);

    return isset($m[0]) ? $m[0] : array();   
}

$test_string = 'This is a test string...

        test1@example.org

        Test different formats:    
        test2@example.org;
        <a href="test3@example.org">foobar</a>
        <test4@example.org>

        strange formats:        
        test5@example.org
        test6[at]example.org
        test7@example.net.org.com
        test8@ example.org
        test9@!foo!.org

        foobar   
';

print_r(extract_emails($test_string));

/*

    Returns this array:

    Array
    (
        [0] => test1@example.org
        [1] => test2@example.org
        [2] => test3@example.org
        [3] => test4@example.org
        [4] => test5@example.org
        [5] => test7@example.net.org.com
    )

*/

Quelle: jonasjohn.de

Sonderzeichen aus String entfernen

Mit dieser Regular Expression kann man auf einfache Weise Sonderzeichen aus einem String entfernen.

preg_replace('/[^a-z0-9]/i', '_', $string)

Ein Bild in einen base64 String umwandeln

Dieses Codebeispiel zeigt, wie man einen Bild in einen base64 String umwandelt

<title>Image to Base64 String</title>
<fieldset>
<legend>Image to Base64 String</legend>
<center>
<form name="select_all">
/**
* @author vir0e5 a.k.a banditc0de
* @copyright 2010 by vir0e5
* @authorurl http://facebook.com/vir0e5.vbs
* @Blog http://banditc0de.blogspot.com
This code will help you to learn how we can convert an image into a base64 string!!
*/
echo"<h3><p>Image</p></h3>";
//$file = File Image yang ingin di encode
//Filetype: JPEG,PNG,GIF
$file = "encode.jpg";
if($fp = fopen($file,"rb", 0))
{
$gambar = fread($fp,filesize($file));
fclose($fp);

$base64 = chunk_split(base64_encode($gambar));
//Result
$encode = '<img src="data:image/jpg/png/gif;base64,' . $base64 .'" >';
echo $encode;
}
?>
<br><textarea name="text_area" rows="20" cols="70"><? echo $encode; ?></textarea>
<p><input type="button" value="Select All Code" onClick="javascript:this.form.text_area.focus();this.form.text_area.select();"></p>
</form>
</center>
</fieldset>[/php]
Quelle

Ein Bild in einen Base64 String wandeln

<title>Image to Base64 String</title>
<fieldset>
<legend>Image to Base64 String</legend>
<center>
<form name="select_all">
/**
* @author vir0e5 a.k.a banditc0de
* @copyright 2010 by vir0e5
* @authorurl http://facebook.com/vir0e5.vbs
* @Blog http://banditc0de.blogspot.com
This code will help you to learn how we can convert an image into a base64 string!!
*/
echo"<h3><p>Image</p></h3>";
//$file = File Image yang ingin di encode
//Filetype: JPEG,PNG,GIF
$file = "encode.jpg";
if($fp = fopen($file,"rb", 0))
{
$gambar = fread($fp,filesize($file));
fclose($fp);

$base64 = chunk_split(base64_encode($gambar));
//Result
$encode = '<img src="data:image/jpg/png/gif;base64,' . $base64 .'" >';
echo $encode;
}
?>
<br><textarea name="text_area" rows="20" cols="70"><? echo $encode; ?></textarea>
<p><input type="button" value="Select All Code" onClick="javascript:this.form.text_area.focus();this.form.text_area.select();"></p>
</form>
</center>
</fieldset>[/php]
Quelle: phpsnips

String nach Hex konvertieren und umgekehrt


/* Convert string to hex */
function str_to_hex($string){
$hex='';
for ($i=0; $i < strlen($string); $i++){
$hex .= dechex(ord($string[$i]));
}
return $hex;
}

/* Convert hex to string */

function hex_to_str($hex){
$string='';
for ($i=0; $i < strlen($hex)-1; $i+=2){
$string .= chr(hexdec($hex[$i].$hex[$i+1]));
}
return $string;
}

// example :

$hex = str_to_hex("test sentence...");
// $hex contains 746573742073656e74656e63652e2e2e

print hex_to_str($hex);
// outputs: test sentence...
?>[/php]

Mit PHP eine Datei in einen String einlesen

Einlesen einer Textdatei in eine String-Variable mittels fgets.

 
// file example 1: read a text file into a string with fgets

$filename="input.txt";
$output="";
$file = fopen($filename, "r");
while(!feof($file)) {

//read file line by line into variable
$output = $output . fgets($file, 4096);

}
fclose ($file);
echo $output;

?>[/php]