Jump to content

preg match help


devWhiz

Recommended Posts

so I use file_get_contents to get the contents of a webpage, one of the lines has

 

 

<meta property="og:url" content="http://www.google.com/blahblahblah?s=5cd04d4a7632296b9cdb463d04e82c05" />

 

I want it to echo only http://www.google.com/blahblahblah so anything between <meta property="og:url" content=" and ?s=5cd04d4a7632296b9cdb463d04e82c05" /> I want to extract and put into a variable

 

this works just fine but I was thinking preg match would be better

 

$thread = explode('<meta property="og:url" content="', $psuc);
$thread = explode('?s=', $thread[1]);
echo $thread[0];

 

Thanks

Link to comment
Share on other sites

preg_match (regex) can help you out better than what you have now, as far as taking into consideration non-standardized formatting.  Things like...

 

- attribute order (ex: content=".." comes before property="..")

- spacing between = signs inconsistent (ex: content = "..." vs. content="...")

- use of single instead of double quotes (ex: content='...' vs. content="...")

- differences in capitalization (ex: CONTENT="..." vs. content="...")

- handling when there is no query string on the URL

 

But what would be even better is if you use a DOM parser to parse html content. 

 

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.