Extract attribute value with Regular Expression?

This is a discussion on "Extract attribute value with Regular Expression?" within the PHP Forum section. This forum, and the thread "Extract attribute value with Regular Expression? are both part of the Program Your Website category.


 Subscribe in a reader

Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices




Reply
 
LinkBack Thread Tools
  #1  
Old May 17th, 2007, 17:17
New Member
Join Date: May 2007
Location: Australia
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Question Extract attribute value with Regular Expression?

Hi

My PHP is a little rusty, as are my regular expressions.

I have a html string held in a variable in php:
Code: Select all
<img src="blah.jpg" alt="what I want" border="0"/>
What is the simplest method to extract the value of the alt attribute?

Thanks
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote

  #2  
Old May 18th, 2007, 06:16
New Member
Join Date: May 2007
Location: Sendai, Japan
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Extract attribute value with Regular Expression?

This might work:
PHP: Select all

<?php 
$img
='<img src="blah.jpg" alt="what I want" border="0"/>';
$alt=preg_replace('/.*alt="(.*!")".*/U','$1',$img);
echo 
$alt;
?>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #3  
Old May 18th, 2007, 08:37
New Member
Join Date: May 2007
Location: Australia
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Extract attribute value with Regular Expression?

Got it to work after a couple of changes, thanks.

Code: Select all
<?php 
$img='<img src="blah.jpg" alt="what I want" border="0"/>';
$alt=preg_replace('/.*alt="([^"]*)"[^.]*/','$1',$img);
echo $alt;
?>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Tags
attribute value, regular expression

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Extract text from a dropdown field? Hmm... simbo1231 JavaScript Forum 4 Nov 4th, 2007 22:40
Using Font Size in BODY attribute nne Website Planning 7 Sep 11th, 2007 11:35
Unicode Regular Expression won't work ?? stacioanri JavaScript Forum 2 Jul 28th, 2007 21:52
Help spliting string by regular expression Sagaris PHP Forum 8 May 1st, 2007 20:46
need help to extract javascript from site. hmc JavaScript Forum 0 Nov 2nd, 2005 07:11


All times are GMT. The time now is 16:08.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42