ozzy47 Posted January 6, 2012 Share Posted January 6, 2012 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']); Quote Link to comment https://forums.phpfreaks.com/topic/254455-str_replace/ Share on other sites More sharing options...
Labradoodle-360 Posted January 6, 2012 Share Posted January 6, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/254455-str_replace/#findComment-1304694 Share on other sites More sharing options...
ragax Posted January 6, 2012 Share Posted January 6, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/254455-str_replace/#findComment-1304700 Share on other sites More sharing options...
ozzy47 Posted January 6, 2012 Author Share Posted January 6, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/254455-str_replace/#findComment-1304704 Share on other sites More sharing options...
ragax Posted January 6, 2012 Share Posted January 6, 2012 Sorry, don't know. Quote Link to comment https://forums.phpfreaks.com/topic/254455-str_replace/#findComment-1304705 Share on other sites More sharing options...
Pikachu2000 Posted January 6, 2012 Share Posted January 6, 2012 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 . . . Quote Link to comment https://forums.phpfreaks.com/topic/254455-str_replace/#findComment-1304708 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.