Hi All, Can any one help me to breaking String using regular expression or any other string method. $Str= "1. hello this is php page. 2. I believe PHP is the BEST section for this topic 3. hello world please solve this problem" Ans should be: 1. hello this is php page. 2. I believe PHP is the BEST section for this topic 3. hello world please solve this problem Thanks Sam
preg_match_all('/[0-9]+\.[a-zA-Z \.]+/U',$stringtosearchin,$result); print_r($result); PHP: Something like that should work (untested). Edit: This won't work if there are numbers in the title, if so, it has to be done manually. Peace,
Sorry: preg_match_all('/[0-9]+\.([a-zA-Z \.])+/U',$stringtosearchin,$result); print_r($result); //$result[1] should contain the info. PHP: Peace,
$Str = "1. hello this is php page. 2. I believe PHP is the BEST section for this topic 3. hello world please solve this problem"; echo sprintf('<pre>%s</pre>', preg_replace('#(\d+\. +)#', '$1'."\n", $Str)); Code (markup):
Hi All, I need some more help on this script. The output of above function is this 1. hello this is php page. 2. I believe PHP is the BEST section for this topic 3. hello world please solve this problem i need output like this 1. hello this is php page. 2. I believe PHP is the BEST section for this topic 3. hello world please solve this problem can you help me for writing this script.. Regard, Sam
Try this. <?php $Str= "1. hello this is php page. 2. I believe PHP is the BEST section for this topic 3. hello world please solve this problem"; $match = preg_split('/([0-9]+)\./i', $Str); $ar = array(); foreach ($match as $el) { $el = trim($el); if ($el != '') { $ar[] = $el; } } unset($match); echo('<pre>' . print_r($ar, true) . '</pre>'); ?> <!-- Now with OL --> <ol> <?php foreach ($ar as $s) : ?> <li><?php echo($s) ?></li> <?php endforeach ; ?> </ol> PHP:
Sorry about that, I put the newline on the wrong side. $Str = "1. hello this is php page. 2. I believe PHP is the BEST section for this topic 3. hello world please solve this problem"; echo sprintf('<pre>%s</pre>', preg_replace('#(\d+\. +)#', "\n".'$1', $Str)); Code (markup): You may or may not want to trim the result if the first newline is a problem for you.. $Str = "1. hello this is php page. 2. I believe PHP is the BEST section for this topic 3. hello world please solve this problem"; echo sprintf('<pre>%s</pre>', trim(preg_replace('#(\d+\. +)#', "\n".'$1', $Str))); Code (markup):
Thank you very much.. $Str= "1. hello this is php page. 2. I believe PHP is the BEST section for this topic 3. hello world please solve this problem"; $match = preg_split('/([0-9]+)\./i', $Str); unset($match[0]); foreach ($match as $s) : $i=(trim($s)); echo '<li>'.$i .'</li>'; endforeach ; ?> 100% running this code.. Thanks a lot.. Regards, Sam
I've updated the code That's the old one (there is a bug in the old one i think). You can check my post.
Hi i think there is no use of this function. $ar = array();foreach ($match as $el) { $el = trim($el); if ($el != '') { $ar[] = $el; }} echo('<pre>' . print_r($ar, true) . '</pre>'); I am sending new updated solution.. sorry for intrupting.. it will display output like this
If you use my old code, and the $Str looks like this : (note that the 4th has nothing). $Str= "1. hello this is php page. 2. I believe PHP is the BEST section for this topic 3. hello world please solve this problem 4. 5. a"; PHP: You'll get a display "bug" <ol> <li> hello this is php page. </li> <li> I believe PHP is the BEST section for this topic </li> <li> hello world please solve this problem </li> <li> </li> <!-- HERE is the empty --> <li> a</li> </ol> PHP: With the new code, empty element(s) will be removed <ol> <li> hello this is php page. </li> <li> I believe PHP is the BEST section for this topic </li> <li> hello world please solve this problem </li> <li> a</li> </ol> PHP: