| AND OR | string words | *

string handling

|

string replace

| see demonstration of string replace


some useful functions to replace strings in strings



full source of string replace [ line 1 - 34 ] | download string replace

1    <?php
2   // Multiple string replace
3   // expects an array with $string => $replacement
4   $old_string 'file_example.txt<br>another_file.txt<br>phpexample.php<br>';
5   $replacements array('_'=>' ','<br>'=>' ,','.txt'=>' (Text)','.php'=>' (PHP)');
6   $new_string strtr($old_string,$replacements);
7   echo $new_string;
8   echo '<hr>';
9   // Number Padding, PHP
10   // Doesn't have to be zeros or numberic but handy for making sure you keep leading zeros on
11   // day and month values when receiving such values as POST or GET variables.
12   $no_a 3;
13   $no_a str_pad($no_a2'0'STR_PAD_LEFT);
14   echo $no_a;
15   echo '<hr>';
16   // String replacing, PHP
17   $old_string 'the qick brown fox jumped over the lazy dog.';
18   $new_string str_replace('qick brown fox''slow black bear'$old_string);
19   echo $new_string;
20   echo '<hr>';
21   // Remove tags using preg_replace, PHP
22   // This should remove anything and everything everything between
23   // 'less than' and 'greater than' characters
24   $source_string '<b>bold</b> <a href="blank.php">link</a>';
25   $replacement '(tag removed)';
26   $replaced_string preg_replace('[<.*?.>]'$replacement$source_string);
27   echo $replaced_string;
28   echo '<hr>';
29   // preg_replace can be used with arrays as well
30   $string "The quick brown fox jumped over the lazy dog.";
31   $patterns array('/quick/','/brown/','/fox/');
32   $replacements array('slow','black','bear');
33   echo preg_replace($patterns$replacements$string);
34   ?>



11 hits by 8 users in the last 30 minutes.