Jump to content

search and replace in preg_replace


robbydweb

Recommended Posts

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

<?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(
"'\[&#039;b&#039;\](.*?)\[/&#039;b&#039;\]'is" => "<b>\\1</b>",
"'\[&#039;i&#039;\](.*?)\[/&#039;i&#039;\]'is" => "<i>\\1</i>",
"'\[&#039;u&#039;\](.*?)\[/&#039;u&#039;\]'is" => "<u>\\1</u>",
);
//the rest of the code

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.