Jump to content

[SOLVED] Regex Problem


jjacquay712

Recommended Posts

this regex should return test.html, but its getting this error: Warning: preg_match_all() [function.preg-match-all]: Delimiter must not be alphanumeric or backslash in lol.php on line 6

<?php
$code = "<a href=\"test.html\">test</a>";
preg_match_all("\/href=\"(.*)\"/", $code, $array);
echo '<pre>';
print_r($array);
echo '</pre>';
?>

Link to comment
Share on other sites

mjdamato: That will work find for a the example give but will fail for this

<a href="test.html" class="myclass">test</a>

 

use lazyiness

'/href="(.*?)"/i'

ie

preg_match_all("/href=\"(.*?)\"/i", $code, $array);

 

and on another note jjacquay712 read the rules.. wait an hour before bumping, if you don't get help after 2 bumps you may want to re-think the question posted,

 

many members scan down the page looking for people with no replies in an effort to help, by bumping yourself too soon mean some members (myself included) would probably miss you and thus costing you more time.

 

in anycase i hope it helps

Link to comment
Share on other sites

That would be true if we used single quotes CV..

 

with double quote we still need to escape the quotes

ie

<?php
$code= '<a href="test.html" class="myclass">test</a>';
preg_match("/href=\"(.*?)\"/i", $code, $array);
echo $array[1];
?>

 

But to save confusion it is always better to contain the RegEx in singles

<?php
$code= '<a href="test.html" class="myclass">test</a>';
if (preg_match('/href="(.*?)"/i', $code, $regs))
{
$result = $regs[1];
}else{
    $result = "Not found";
}
echo $result;
?>

Link to comment
Share on other sites

What I mean is, if you're going to for example scrape a webpage using file_get_contents, you wouldn't be using quotes to assign the data to a var at all, so quoting is not an issue.  But in the regex above, you are taking into consideration escaped quotes, which would be non-existent in a scraped page from file_get_contents, since the data would already be rendered.

 

Point is, when you are trying to create a regex pattern using example data in a string, you're going to have to do some quote juggling, be it escaping, or using single/double, or whatever.  But that won't really necessarily be the case/issue for where your data is coming from in production environment.

 

Look at the OP's example string:

 

$code = "<a href=\"test.html\">test</a>";

 

It would not look like that if that link was coming from a page scrape.  It would not be escaped like that.  But he made his regex to look for escaped quotes. 

Link to comment
Share on other sites

use lazyiness

'/href="(.*?)"/i'

 

Even better, be specific: '/href="([^"]*)"/i'

 

in the regex above, you are taking into consideration escaped quotes

 

The double quotes were escaped in the regex because they were used to delimit the string; they will match an unescaped quote:

 

<pre>
<?php
  $str = '"';
  echo preg_match("/\"/", $str);
?>
</pre>

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.