robbydweb Posted March 26, 2007 Share Posted March 26, 2007 Im trying to figure out how to search replace the var in a preg_replace This is the basic code: $search = array( '#\[item\](.+?)\[\/item\]#mis', ); $replace = array( '<a href="page.php?item=$1">$1</a>', ); $link = preg_replace ( $search, $replace, $link ); However if the 'item' has apostrophe in the '$1' I want that replaced as well. Anyone have ideas on how to do that? Link to comment https://forums.phpfreaks.com/topic/44417-search-and-replace-in-preg_replace/ Share on other sites More sharing options...
desithugg Posted March 27, 2007 Share Posted March 27, 2007 <?php $bbcode = array( "'\[b\](.*?)\[/b\]'is" => "<b>\\1</b>", "'\[i\](.*?)\[/i\]'is" => "<i>\\1</i>", "'\[u\](.*?)\[/u\]'is" => "<u>\\1</u>", ); //'is' in the array are there to make it not case sensitive and to make the code //still woke if the code goes on a new line like [b //] know what i mean? //anyways $whatever = nl2br(preg_replace (array_keys($bbcode), array_values($bbcode), $whatever)); //as for the commas thing im not sure but you can try $whatever = htmlspecialchars($whatever, ENT_QUOTES); //r $bbcode = array( "'\['b'\](.*?)\[/'b'\]'is" => "<b>\\1</b>", "'\['i'\](.*?)\[/'i'\]'is" => "<i>\\1</i>", "'\['u'\](.*?)\[/'u'\]'is" => "<u>\\1</u>", ); //the rest of the code Link to comment https://forums.phpfreaks.com/topic/44417-search-and-replace-in-preg_replace/#findComment-215841 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.