hi, I have this string: $mystr = "<ul>bla bla bla bla...</ul>"; Code (markup): I want to find the "</ul>" and replace it with "</table>" but to print only this and to exclude any other text before or after that. how i can do this? Thank in advance.
Best way to solve this is with the following code: $mystr = "<ul>bla bla bla bla...</ul>"; $mystr = str_replace(array('<ul>', '</ul>'), array('<table>', '</table>'), $mystr); PHP: Or if you only want the </ul>: $mystr = "<ul>bla bla bla bla...</ul>"; $mystr = str_replace('</ul>', '</table>', $mystr); PHP:
You can use following function to find and replace <?php function str_replace_once($search, $replace, $subject) { $firstChar = strpos($subject, $search); if($firstChar !== false) { $beforeStr = substr($subject,0,$firstChar); $afterStr = substr($subject, $firstChar + strlen($search)); return $beforeStr.$replace.$afterStr; } else { return $subject; } } ?> PHP: