Jump to content

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


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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.