Asked  11 Months ago    Answers:  5   Viewed   6 times

I'm attempting to run preg_match to extract the SRC attribute from the first IMG tag in an article (in this case, stored in $row->introtext).

preg_match('/< *[img][^>]*[src] *= *["']{0,1}([^"']*)/i', $row->introtext, $matches);

Instead of getting something like



<img src="images/stories/otakuzoku1.jpg" border="0" alt="Inside Otakuzoku's store" />

I get just


The regex should be right, but I can't tell why it appears to be matching the border attribute and not the src attribute.

Alternatively, if you've had the patience to read this far without skipping straight to the reply field and typing 'use a HTML/XML parser', can a good tutorial for one be recommended as I'm having trouble finding one at all that's applicable to PHP 4.

PHP 4.4.7



Your expression is incorrect. Try:

preg_match('/< *img[^>]*src *= *["']?([^"']*)/i', $row->introtext, $matches);

Note the removal of brackets around img and src and some other cleanups.

Friday, July 2, 2021

[Edited (again) to improve formatting and fix the intro.]

This is a comment and an answer.

The answer part... I do agree with alex' earlier answer.

  1. (?: ), in contrast to ( ), is used to avoid capturing text, generally so as to have fewer back references thrown in with those you do want or to improve speed performance.

  2. The ? following the (?: ) -- or when following anything except * + ? or {} -- means that the preceding item may or may not be found within a legitimate match. Eg, /z34?/ will match z3 as well as z34 but it won't match z35 or z etc.

The comment part... I made what might considered to be improvements to the regex you were working on:


-- First, it avoids things like 0-0-2011

-- Second, it avoids things like 233443-4-201154564

-- Third, it includes things like 1-1-2022

-- Forth, it includes things like 1-1-11

-- Fifth, it avoids things like 34-4-11

-- Sixth, it allows you to capture the day, month, and year so you can refer to these more easily in code.. code that would, for example, do a further check (is the second captured group 2 and is either the first captured group 29 and this a leap year or else the first captured group is <29) in order to see if a feb 29 date qualified or not.

Finally, note that you'll still get dates that won't exist, eg, 31-6-11. If you want to avoid these, then try:


Also, I assumed the dates would be preceded and followed by a space (or beg/end of line), but you may want ot adjust that (eg, to allow punctuations).

A commenter elsewhere referenced this resource which you might find useful:

Saturday, May 29, 2021

Use content:url("image.jpg").

Full working solution (Live Demo):

<!doctype html>


<img class="MyClass123"/>

Tested and working:

  • Chrome 14.0.835.163
  • Safari 4.0.5
  • Opera 10.6

Tested and Not working:

  • FireFox 40.0.2 (observing Developer Network Tools, you can see that the URL loads, but the image is not displayed)
  • Internet Explorer 11.0.9600.17905 (URL never loads)
Tuesday, June 1, 2021

Your regex will break if the string $id_base has a / in it as you are using / as the regex delimiter.

To fix this use preg_quote on $id_base as:

if (preg_match('/'. preg_quote($id_base,'/').'-([0-9]+)$/', .....) {
Saturday, December 25, 2021
document.getElementById("outmessage").innerHTML = "<h2> Image <img src= "+src3+" /></h2>";

The above works in the browsers (ie/firefox etc). i was running the same pieces of code through eclipse (deploying on tomcat) and was checking the result page on eclipse default browser.There it was not displaying the image. When i ran the same url in firefox or chrome,it displayed the image. I guess some problem with eclipse default browser not being able to show the image

Wednesday, December 29, 2021
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :