PDA

View Full Version : yet another replace issue php, img to div


Dr.CustUmz
03-17-2016, 05:05 PM
so i have been playing with this for a couple days, and i still cant get it. im trying to replace the avatar img in postbit to be a div instead.

this is the line to be changed:
<img src="$post[avatarurl]" $post[avwidth] $post[avheight] alt="<phrase 1="$post[username]">$vbphrase[xs_avatar]</phrase>" border="0" />

im hooking into postbit_display_complete

some methods i have used with no results:
$search = '<img src="$post[avatarurl]" $post[avwidth] $post[avheight] alt="<phrase 1="$post[username]">$vbphrase[xs_avatar]</phrase>" border="0" />';
$replace = '<div id="drcltrav" style="background-color:'.$color.';"><span>'.strtoupper($drcltrav_lgth).'</span></div>';
$vbulletin->templatecache['postbit'] = str_replace($search, $replace, $vbulletin->templatecache['postbit']);
$show['avatar'] = true;

using for my search:
$search = '<img src="' . $post[avatarurl] . '" ' . $post[avwidth] . ' ' . $post[avheight] . 'alt="<phrase 1="' . $post[username] . '">' . $vbphrase[xs_avatar] . '</phrase>" border="0" />';

this method seemed the most promising since it wouldnt matter if the site has a custom template:
$pattern = '/<img src="'.$post['avatarurl'].'"(.*?)>/';
$replacement = '<div id="myDiv">content</div>';

echo preg_replace($pattern, $replacement, $vbulletin->templatecache['postbit']);

and they're about 50 other ways I have tried this. the ONLY time i get a result is when i use
$post['avatarurl'] = '" class="someClass';

but i dont just want to add a class to it i want to replace it completely

squidsk
03-17-2016, 06:26 PM
Your playing around with regular expressions so you need to escape every non-standard character otherwise they all have particular meanings for the regular expression engine other than their plain text characters.

So your search string would need to be something like:

'\<img src=\"\$post\[avatarurl\]\" \$post\[avwidth\] \$post\[avheight\] alt=\"\<phrase 1=\"\$post\[username\]\"\>\$vbphrase\[xs_avatar\]\<\/phrase\>\" border=\"0\" \/\>'

The easiest way to test is to use an online preg_replace tester to see if you patterns work or not.

Dr.CustUmz
03-17-2016, 06:48 PM
Your playing around with regular expressions so you need to escape every non-standard character otherwise they all have particular meanings for the regular expression engine other than their plain text characters.

So your search string would need to be something like:

'\<img src=\"\$post\[avatarurl\]\" \$post\[avwidth\] \$post\[avheight\] alt=\"\<phrase 1=\"\$post\[username\]\"\>\$vbphrase\[xs_avatar\]\<\/phrase\>\" border=\"0\" \/\>'

The easiest way to test is to use an online preg_replace tester to see if you patterns work or not.

so what would i have to escape if i were using
$pattern = '/<img src="'.$post['avatarurl'].'"(.*?)>/';
$replacement = '<div id="myDiv">content</div>';

i tried a tester but it threw me errors no matter what i did =/

squidsk
03-17-2016, 08:44 PM
Try <img src\="\$post\[avatarurl\]".*/> as the pattern.

Dr.CustUmz
03-17-2016, 09:50 PM
$pattern = '<img src\="\$post\[avatarurl\]".*/>';
$replacement = '<div id="myDiv">content</div>';
echo preg_replace($pattern, $replacement, $vbulletin->templatecache['postbit']);

no luck =(

MarkFL
03-17-2016, 10:38 PM
Try the pattern:

$pattern = '/<img src\="\$post\[avatarurl\]".*?\/>/';

Dr.CustUmz
03-17-2016, 11:41 PM
again nothing, lol this is becoming such a headache for such a little task =(

--------------- Added 1458277099 at 1458277099 ---------------

just for popped in my head so i tried.

postbit_display_complete
$search = 'img';
$replace = 'a';

$vbulletin->templatecache['postbit'] = preg_replace($search, $replace, $vbulletin->templatecache['postbit']);

seems like that should do something.... like replace all the images with links, but all it does is cause an infinite load -_-

--------------- Added 1458322966 at 1458322966 ---------------

seems postbit isnt cached in the hook i was using

using
$vbulletin->templatecache['postbit'] = str_replace('<img src=\"$post[avatarurl]', 'SHIT', $vbulletin->templatecache['postbit']);

in global gave me results. although i need some help to get the "whatever comes after doesnt matter" combination, been playing with it for hours and cant get it =/

example in red:
str_replace('<img src=\"$post[avatarurl]'\"*/>, ......

so everything in the img tag gets replaced.