FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


help with pcre syntax





roboguyspacedude
ok, I want to search a string for the <img src="url" Anything after this> tag and replace it with [img]url[/img].
can somebody tell me the correct syntax for the pcre?
I have:
Code:

$simple_search = array(
'/<img src\="(.*?)">/is'
);
$simple_replace = array(
'[img]$1[/img]'
);
  $str = preg_replace ($simple_search, $simple_replace, $str);
MrBlueSky
Try something like this:

Code:

'/<img[^>]+?src\\s*=\\s*[\'"]?([^\'" >]+?)[ \'"]?>/si'


It matches, for example:

<img src = "wite.com'>
<img src=htt://www.site.com/test.jpg>
<img target="_blanc" src= pic.gif >
<IMG class="image" width=10px SRC = 'some.png'>

It doesn't match:

<img src="image" class="someclass">

Because the src-parameter must be last.

--edit--

By the way, you don't need the array's if you just want to replace in one string:

Code:

$result = preg_replace('/<img[^>]+?src\\s*=\\s*[\'"]?([^\'" >]+?)[ \'"]?>/i', '[img]$1[/img]', $str);
roboguyspacedude
I tried yours but it seemed to only work for no quotes around the url.
I tried
Code:
$str = preg_replace('/<img[^>]src=["]?([^\'">]+?)["]?>/si' , '[img]$1[/img]', $str);

just to see if it would work, but that doesn't work either, though it should be able to do <img src="">
MrBlueSky
Thats weird. It should work. I've put up a test here: http://www.mrbluesky.frih.net/test/regex.php

I used this as the source code:

Code:

<?php
error_reporting(E_ALL);

// text to be scanned is
$string = <<<EOF
<img src = "wite.com'>
<img src=   htt://www.site.com/test.jpg>
<img target="_blanc" src= pic.gif >
<IMG class="image" width=10px SRC = 'some.png'>
EOF;

// search sample with delimiter and modifer (if available)
$pattern = '/<img[^>]+?src\\s*=\\s*[\'"]?([^\'" >]+?)[ \'"]?>/is';

// replacement
$ersatz = '[img]$1[/img]';

// regex with preg_replace() evaluate
echo preg_replace($pattern, $ersatz, $string );

?>


Can you post the the part of your program where you use the regex?
roboguyspacedude
actually i found out that the php form is automatically converting the quotes to \" (i guess so that it adds to mysql alright) so i need the pcre syntax that you have above, just have it search for \" instead of ". so for example <img src=\"\">
MrBlueSky
You can use the function stripslashes() to remove the added extra quotes:

Code:

$str = stripslashes($str);
$result = preg_replace('/<img[^>]+?src\\s*=\\s*[\'"]?([^\'" >]+?)[ \'"]?>/si', '[img]$1[/img]', $str);


(The extra slashes in form-data are added by PHP when magic_quotes_gpc is set to on, which is the case at frihost)
Stubru Freak
roboguyspacedude wrote:
actually i found out that the php form is automatically converting the quotes to \" (i guess so that it adds to mysql alright) so i need the pcre syntax that you have above, just have it search for \" instead of ". so for example <img src=\"\">


That's magic_quotes_gpc, it's a bad thing and you should turn it off in your .htaccess file by adding this line:
php_flag magic_quotes_gpc Off
roboguyspacedude
thank you so much guys, it works now Smile
---Edit---
Is there any search syntax that will allow for
Code:
<img width="100" src="" alt="">
, or is the only possibility the way you said.
Related topics
Help with mysql query
Problem with MySQL Server Please Help!!
Php Syntax Errors, Need Help
I could really use some help with Javascript...
Php parsing error! Need help
Parse error - need help
mysql syntax error I can't find why
Need help fixing my php file
Help with javascript!!
50fri$ Please help me with curl.
Error while creating the SQL table pls help
Need help with This ASAP!
PlZ help - AlstraSoft Article Manager Pro
Help with regular expression
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.