close ad
Important WebAssist Announcement
open ad
View Menu

Technical Support Forums

Free, outstanding support from WebAssist and your colleagues


failing to properly access array created by preg_match

Thread began 1/30/2022 5:31 am by yogastudents362523 | Last modified 1/31/2022 1:17 pm by Ray Borduin | 340 views | 4 replies |


failing to properly access array created by preg_match


You may recall that as per this code snippet:

<?php $shortbio = preg_replace("/<img[^>]+\>/i", "", $rsAllPresenterData->getColumnVal("Biography",false)) ; ?>
<?php $shortbio = substr($shortbio, 0, 550) ; ?>

you assisted me with the preg_replace function. (I had foolishly placed the substr request BEFORE the remove references to images request; and you fixed my issue by inverting the code order; and as given above).

I am now having a problem with the complementary preg_match function.

preg_match is supposed to examine the data, count the number of matching instances, and return an array of all images in the provided HTML code snippet. But either I am not receiving that array; or else the code I write is mistaken (more likely).

I am providing workable material, because:
echo($rsAllPresenterData->getColumnVal("Biography",false)) ;

correctly displays Presenter Bios on the page, including all images.

I then use preg_match as follows.
(I felt it best to leave my little foolish 'echo' notes to myself in place so you can correct my logic if necessary):

echo('This is before the preg_match and its print_r, thus indicating success in matching<br />');

print_r(preg_match("/<img[^>]+\>/i", $rsAllPresenterData->getColumnVal("Biography",false), $matches, PREG_OFFSET_CAPTURE)) ;

echo('<br />And this is before the print_r of those $matches <br />');

print_r($matches) ;

echo('<br />This is after all that<br /> and then here, supposedly, is the foreach<br /> ');

foreach($matches as $myinfo)

The first print_r displays a '1' (to show success in matching of some kind).

The second print_r displays only the first image in the bio, along with the text:
Array ( [0] => Array ( [0] => [1] => 0 ) )

And the third print_r again displays only the first image in the bio, along with the shorter text:
Array ( [0] => [1] => 0 )

I have attached the file concerned.

Thank you,

(And in case it makes a difference, then I am trying to create a dynamic menu (option-select) containing the precise names of all the image files Presenter use in their longer biographies.
I am already extracting the first 500 characters, without images, of their long bio to create a more general listing of all Presenters. But that short bio nevertheless needs an image. I at first thought I could leave them to copy and paste the relevant filename into a datbase field and update the database. But as you will appreciate, this has its rather large issues. I need an easier and more automatic process. A menu will faultlessly and accurately contain the precise filenames of all images in their bio, and they can then select to indicate which image they want me to use. I already have coding to place the correct image from the filemanager's thumbs directory in place. Thank you. But I can't get the above preg_match to work. Thank you).

Attached Files
Sign in to reply to this post

Ray BorduinWebAssist

You will have to use preg_match_all() instead of preg_match() to return all of the images and not just the first one. If you still have issues, please include a url with steps to see the problem so I can debug it live.

Sign in to reply to this post
Did this help? Tips are appreciated...


Thank you, thank you: most sincerely.

I changed the regular expression and now have the following coding:

preg_match_all('@src="([^"]+)"@', $rsAllPresenterData->getColumnVal("Biography",false), $matches, PREG_OFFSET_CAPTURE);

$controller = array_pop($matches) ;

foreach($controller as $myinfo)

I am now very close indeed because the result I am getting is:

Array ( [0] => [1] => 64 )
Array ( [0] => [1] => 571 )
Array ( [0] => [1] => 834 )

etc. etc.

chopping off the necessary front characters is obviously trivial using standard string manipulations.
But chopping off the back few (when they are so variable) is proving more of a challenge.

I tried using " [1] => " for a further reg ex comparison. I was then hoping to strip everything beyond that point away to the end of that string by dint of replacing it with "", but that is not currently working.

I am now researching ... but do you happen to know of a PHP routine that specializes in tailend of string recognition?

Thank you,

Sign in to reply to this post

Ray BorduinWebAssist

You just have to use $myinfo[0]

I updated the page for an example.

Sign in to reply to this post
Did this help? Tips are appreciated...


Thank you.
I again feel so stupid!
Ah well. I guess, at least, I got the regex in situ that makes possible what I want to do..

I have downloaded the page.
Thank you again.
I can now finish this, and then move on to the next problem.

With every good wish,

Sign in to reply to this post

Build websites with a little help from your friends

Your friends over here at WebAssist! These Dreamweaver extensions will assist you in building unlimited, custom websites.

Build websites from already-built web applications

These out-of-the-box solutions provide you proven, tested applications that can be up and running now.  Build a store, a gallery, or a web-based email solution.

Want your website pre-built and hosted?

Close Windowclose

Rate your experience or provide feedback on this page

Account or customer service questions?
Please user our contact form.

Need technical support?
Please visit support to ask a question





Ease of use


security code refresh image

We do not respond to comments submitted from this page directly, but we do read and analyze any feedback and will use it to help make your experience better in the future.

Close Windowclose

We were unable to retrieve the attached file

Close Windowclose

Attach and remove files

add attachmentAdd attachment
Close Windowclose

Enter the URL you would like to link to in your post

Close Windowclose

This is how you use right click RTF editing

Enable right click RTF editing option allows you to add html markup into your tutorial such as images, bulleted lists, files and more...

-- click to close --

Uploading file...