Jump to content

str_replace


ozzy47

Recommended Posts

I am trying to code a str_replace but I can not get it to work.

 

This is what I am searching for:

<div class="postbody">

 

And this is what I need it replaced with:

<div class="postbody <vb:if condition="$show['moderated']">moderated</vb:if>">

 

This is how I have to write the str_replace for vbulletin:

$vbulletin->templatecache['postbit_legacy'] = str_replace('SEARCH CODE','REPLACE CODE',$vbulletin->templatecache['postbit_legacy']);

 

 

Link to comment
https://forums.phpfreaks.com/topic/254455-str_replace/
Share on other sites

How are you trying to execute the replace? If you're trying to replace the code client side, you'll need to use JavaScript, or jQuery/AJAX.

 

If you can do it server side (via PHP) great. Here's the PHP code.

$search = '<div class="postbody">';
$replace = '<div class="postbody <vb:if condition="$show[\'moderated\']">moderated</vb:if>">';
if (!empty($your_variable))
str_replace($search, $replace, $your_variable); # Make sure "$your_variable" is an existing variable that contains your HTML.

Link to comment
https://forums.phpfreaks.com/topic/254455-str_replace/#findComment-1304694
Share on other sites

This looks like a straight literal replacement, with no regex to speak of.

You are using str_replace, that is not a regex function (unlike preg_replace).

In that sense, it might be more a vbull question than a regex or php question.

 

Maybe you have formatted SEARCH CODE like a regex:

'/<div class="postbody">/'

 

This would not work in str_replace because it will search  for the delimiters---which aren't there.

For SEARCH CODE, I would try the simple string you supplied:

'<div class="postbody">'

 

Maybe let us know what you have tried and what hasn't worked, and we can help you troubleshoot some more.

 

Wishing you a beautiful day

:)

 

Link to comment
https://forums.phpfreaks.com/topic/254455-str_replace/#findComment-1304700
Share on other sites

I have tried:

$vbulletin->templatecache['postbit_legacy'] = str_replace('<div class="postbody">','<div class="postbody <vb:if condition="$show['moderated']">moderated</vb:if>">".>',$vbulletin->templatecache['postbit_legacy']);

 

Which shows a syntax error in php, unexpected T_STRING

 

I tried this:

$vbulletin->templatecache['postbit_legacy'] = str_replace('<div class="postbody">','<div class="postbody <vb:if condition="$show[\'moderated\']">moderated</vb:if>">".>',$vbulletin->templatecache['postbit_legacy']);

 

Which does not work it shows errors on my page, Parse error: syntax error, unexpected T_STRING

Link to comment
https://forums.phpfreaks.com/topic/254455-str_replace/#findComment-1304704
Share on other sites

The second one has no parse errors, but variables aren't interpolated within single quoted strings, so you'll end up with the array element being a string literal in the output. Maybe that's what you want, or maybe it isn't . . .

Link to comment
https://forums.phpfreaks.com/topic/254455-str_replace/#findComment-1304708
Share on other sites

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.