Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Need help with regexp

Need help with regexp
Thread Tools
Mac Elite
Join Date: Jan 2001
Status: Offline
Reply With Quote
Jan 6, 2006, 02:05 AM
 
I'm trying to write some php to take this:

Code:
<param name="movie" value="lookupSong.swf?s=Start+Wearing+Purple&n=Gogol+Bordello&t=2%3A10+AM&img=http%3A%2F%2F206.165.114.205%2Fjellyvision%2Fimages%2Fpop%2Fcov75%2Fdrf200%2Ff202%2Ff202950ofan.jpg&itid=&ital=" />
And spit out the value of the s, n, and t variables without the +'s and other entities. There are multiple blocks of code like this in the document I am working with, so it needs to get those values for each one.

Any ideas?

Thanks!
     
Grizzled Veteran
Join Date: Oct 2003
Status: Offline
Reply With Quote
Jan 6, 2006, 10:49 AM
 
how about grabbing the value of value="" and parsing it as a url, then parse the query string.

I tested this on PHP 5, but it should also work on PHP 4 if that is what you're running.

Code:
MadMac-Computer% /usr/local/php5/bin/php -a Interactive mode enabled php > // string php > $string = '<param name="movie" value="lookupSong.swf?s=Start+Wearing+Purple&n=Gogol+Bordello&t=2%3A10+AM&img=http%3A%2F%2F206.165.114.205%2Fjellyvision%2Fimages%2Fpop%2Fcov75%2Fdrf200%2Ff202%2Ff202950ofan.jpg&itid=&ital=" />'; php > php > // PCRE pattern php > $pattern = '/<param name="([^"]+)" value="([^"]+)" \/>/'; php > php > // find matches php > preg_match($pattern, $string, $matches); php > php > // debug matches php > print_r($matches); Array ( [0] => <param name="movie" value="lookupSong.swf?s=Start+Wearing+Purple&n=Gogol+Bordello&t=2%3A10+AM&img=http%3A%2F%2F206.165.114.205%2Fjellyvision%2Fimages%2Fpop%2Fcov75%2Fdrf200%2Ff202%2Ff202950ofan.jpg&itid=&ital=" /> [1] => movie [2] => lookupSong.swf?s=Start+Wearing+Purple&n=Gogol+Bordello&t=2%3A10+AM&img=http%3A%2F%2F206.165.114.205%2Fjellyvision%2Fimages%2Fpop%2Fcov75%2Fdrf200%2Ff202%2Ff202950ofan.jpg&itid=&ital= ) php > php > // parse value="?" url php > $url = parse_url($matches[2]); php > php > // debug $url php > print_r($url); Array ( [path] => lookupSong.swf [query] => s=Start+Wearing+Purple&n=Gogol+Bordello&t=2%3A10+AM&img=http%3A%2F%2F206.165.114.205%2Fjellyvision%2Fimages%2Fpop%2Fcov75%2Fdrf200%2Ff202%2Ff202950ofan.jpg&itid=&ital= ) php > php > // parse query string php > parse_str($url['query'], $results); php > php > // debug results php > print_r($results); Array ( [s] => Start Wearing Purple [n] => Gogol Bordello [t] => 2:10 AM [img] => http://206.165.114.205/jellyvision/images/pop/cov75/drf200/f202/f202950ofan.jpg [itid] => [ital] => ) php >
Something like that should do the trick. You could easily make it into a function or class that you give the string to and it returns the final result array. To get all of them, you could do a preg_match_all() instead of preg_match and loop through the results.

PS. PHP 5 interactive console rocks
The only thing necessary for evil to flourish is for good men to do nothing
- Edmund Burke
     
timmerk  (op)
Mac Elite
Join Date: Jan 2001
Status: Offline
Reply With Quote
Jan 7, 2006, 06:22 PM
 
Thanks!
     
   
Thread Tools
Forum Links
Forum 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
Top
Privacy Policy
All times are GMT -5. The time now is 07:47 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2