Jump to content

preg_replace() [function.preg-replace]: Empty regular expression


drisate

Recommended Posts

Hi guys ia m having a hard time making this script work

 

                  $sb_message_txt = $fmessage->message;
                  $sb_message_txt = stripslashes(smile::smileReplace($sb_message_txt,0, $sbConfig['disemoticons']));
                  $sb_message_txt = str_replace("\n","<br />",$sb_message_txt);
                  
preg_match('/(<img.*?src="(.*?)".*?>)/',$sb_message_txt,$match);
$picinfo = getimagesize($match[2]);
$picinfo[0] = ($picinfo[0] > 430)? 430 : $picinfo[0];
$replacement =  'img src="'.$match[2].'" width="'.$picinfo[0].'" border="0" alt="" /';
$sb_message_txt = preg_replace($match[1], $replacement, $sb_message_txt);

 

I am trying to resize every images bigger then 430 px but i get this error

 

Warning: preg_replace() [function.preg-replace]: Empty regular expression in view.php on line 439

 

Line 439 is $sb_message_txt = preg_replace($match[1], $replacement, $sb_message_txt);

$match[1] will not be formatted as a regex, try this.

 

$regex = '/(<img.*?src="(.*?)".*?>)/';
preg_match($regex,$sb_message_txt,$match);
$picinfo = getimagesize($match[2]);
$picinfo[0] = ($picinfo[0] > 430)? 430 : $picinfo[0];
$replacement =  'img src="'.$match[2].'" width="'.$picinfo[0].'" border="0" alt="" /';
$sb_message_txt = preg_replace($regex, $replacement, $sb_message_txt);

hmm it works but for some reason every images of the messages are replaces by the value of the first image

 

img src="http:// img201.imageshack.us /img201/5206/ skellgriffot8.png" width="430" border="0" alt="" /

 

img src="http:// img201.imageshack.us /img201/5206/ skellgriffot8.png" width="430" border="0" alt="" /

 

img src="http:// img201.imageshack.us /img201/5206/ skellgriffot8.png" width="430" border="0" alt="" /

 

img src="http:// img201.imageshack.us /img201/5206/ skellgriffot8.png" width="430" border="0" alt="" /

in a very desprate attempt i tryed

 

$regex = '/(<img.*?src="(.*?)".*?>)/';

preg_match($regex,$sb_message_txt,$match);

foreach($regex as $regexs){

$picinfo = getimagesize($match[2]);

$picinfo[0] = ($picinfo[0] > 430)? 430 : $picinfo[0];

$replacement =  'img src="'.$match[2].'" width="'.$picinfo[0].'" border="0" alt="" /';

$sb_message_txt=preg_replace($regexs, $replacement, $sb_message_txt);       

}

 

LOL Obviously not working hahaha

To match and replace all images in the string, you need to use preg_match_all and loop through each match.  preg_match_all returns a different array structure than preg_match, because it has more than one match.  Read the manual. 

 

<?php
preg_match_all('/(<img.*?src="(.*?)".*?>)/',$message,$match);
foreach($match[2] as $key => $pic) {
   $picinfo = getimagesize($pic);
   $picinfo[0] = ($picinfo[0] > 430)? 430 : $picinfo[0];
   $replacement =  'img src="'.$pic.'" width="'.$picinfo[0].'" height="'.$picinfo[1].'" border="0" alt="" /';
   $message = preg_replace($match[1][$key], $replacement, $message);
} // end foreach

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.