Forum Moderators: coopster
<p>Hello this is a test. <img src="/something/picture.jpg" alt="inauguracion" title="test" class="right" />Some data here... some more other here. Here another img <img src="other.jpg"> <strong>finish</strong>.</p>
I know this must be done with regex but i am totally lost in that.
im using this code:
<?
$subject = '<p>Hello this is a test. <img src="/something/picture.jpg" alt="inauguracion" title="test" class="right" />Some data here... some more other here. Here another img <img src="other.jpg"> <strong>finish</strong>.</p>';
$pattern = '<\s*(img)[^>]*(src)\s*=\s*[quote]\s*([^"]*\/)*[^"\s]+\s*[quote][^>]*>';
preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE, 3);
print_r($matches);
?>
Its throwing me this error:
Warning: preg_match(): Unknown modifier ']' in D:\Websites\clientes\online\nikkeimotors.com\test.php on line 4
<?
$subject = '<p>Hello this is a test. <img src="/something/picture.jpg" alt="inauguracion" title="test" class="right" />Some data here... some more other here. Here another img <img src="other.jpg"> <strong>finish</strong>.</p>';
$pattern = '<\s*img[^>]*src\s*=\s*"\s*([^"]*\/)*[^"\s]+\s*"[^>]*>';
preg_match_all($pattern, $subject, $matches);
print_r($matches);
?>
$pattern = '/<\s*img [^\>]*src\s*=\s*[\""\']?([^\""\'\s>]*)/i';
It works ok but $matches is trhrowing me this data:
Array
(
[0] => <img src="/something/picture.jpg
[1] => /something/picture.jpg
)
I only would like it to throw:
[0] => /something/picture.jpg
What changes should i implement?
Warning: preg_match(): Unknown modifier ']' in D:\Websites\clientes\online\nikkeimotors.com\test.php on line 4Perhaps it has something to do with your note: "
(replacing broken pipes above with vertical bar symbols) " ... i dont get that ...
the verticle bar symbol (aka "pipe" symbol, which is a unix term; aka "or" symbol in some languages such as perl) is the "upper case" for the backwards slash character on my keyboard.
any post made on this site translates the verticle bar to a broken "pipe" symbol.
the most recent regexp example does not contain any pipes.
I added a / as patterns first and last char nd now i dont get any error msgs:$pattern = '/< \s* img [^\>]* src \s* = \s* [\""\']? ( [^\""\'\s>]* )/';
but the $matches var gets nothing at the end
too many spaces added!
they are meaningful in a regular expression.
how you use a regular expression within a script depends on the scripting language and the context in which it is used.
you asked for the regular expression and the regular expression is contained within the slashes in your case.
the problem is that the preg_match function is throwing two values (instead of just one). I am using this pattern:
$pattern = '/<\s*img [^\>]*src\s*=\s*[\""\']?([^\""\'\s>]*)/i';
And this is the result:
Array
(
[0] => <img src="/something/picture.jpg
[1] => /something/picture.jpg
)
I just want the result to be:
Array
(
[0] => /something/picture.jpg
)
I arranged some of the regex chars and it ended up like this:$pattern = '/<\s*img [^\>]*src\s*=\s*[\""\']?([^\""\'\s>]*)/i';
It works ok but $matches is trhrowing me this data:
Array
(
[0] => <img src="/something/picture.jpg
[1] => /something/picture.jpg
)I only would like it to throw:
[0] => /something/picture.jpgWhat changes should i implement?
put your code in a loop and index into the string for each iteration.
then use Array[1] for each iteration since that is what you want, right?
or use preg_match_all as i suggested in my 2nd response and that will get all matches in one shot.
then use the correct array slice depending on how you call it...
i've never programmed in php, so you might want to refer to this:
[us3.php.net...]