jjacquay712 Posted January 20, 2009 Share Posted January 20, 2009 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>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/141673-solved-regex-problem/ Share on other sites More sharing options...
jjacquay712 Posted January 20, 2009 Author Share Posted January 20, 2009 bump Quote Link to comment https://forums.phpfreaks.com/topic/141673-solved-regex-problem/#findComment-741623 Share on other sites More sharing options...
Psycho Posted January 20, 2009 Share Posted January 20, 2009 $code = "<a href=\"test.html\">test</a>"; preg_match_all("/href=\"(.*)\"/i", $code, $array); echo '<pre>'; print_r($array); echo '</pre>'; EDIT: DO not "bump" after only 10 minutes. Quote Link to comment https://forums.phpfreaks.com/topic/141673-solved-regex-problem/#findComment-741625 Share on other sites More sharing options...
MadTechie Posted January 21, 2009 Share Posted January 21, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/141673-solved-regex-problem/#findComment-741653 Share on other sites More sharing options...
.josh Posted January 21, 2009 Share Posted January 21, 2009 note that unless you are retrieving your string from some text file or something where the escape quotes will physically be there, you need to remove the escape quotes from your regex. For example, scraping a rendered webpage will not have escaped quotes in it. Quote Link to comment https://forums.phpfreaks.com/topic/141673-solved-regex-problem/#findComment-741655 Share on other sites More sharing options...
MadTechie Posted January 21, 2009 Share Posted January 21, 2009 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; ?> Quote Link to comment https://forums.phpfreaks.com/topic/141673-solved-regex-problem/#findComment-741665 Share on other sites More sharing options...
.josh Posted January 21, 2009 Share Posted January 21, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/141673-solved-regex-problem/#findComment-741695 Share on other sites More sharing options...
effigy Posted January 21, 2009 Share Posted January 21, 2009 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> Quote Link to comment https://forums.phpfreaks.com/topic/141673-solved-regex-problem/#findComment-742256 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.