Re: Quotes in regular expressions
| From: | Thaddeus J. Quintin | Date: | Thu, 21 Aug 2003 20:33:41 +0000 |
| Subject: | Re: Quotes in regular expressions | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-160349@lists.php.net to get a copy of this message | ||
The biggest part of my problem was that I had already called "htmlspecialchars" on the string, so there was no quotes to match!
Duh.
Even after I fixed that it was still turning into a hassle, so I just made two checks, one for single quotes, and, failing that, one for double quotes.
not the prettiest answer, but I put good comments around it!
Thaddeus
Scott Fletcher wrote:
Try parsing it as a string where the double quote would become a string. See if that help. (Parse the whole characters into strings then find the double quote.) "Thaddeus J. Quintin" <thaddeus@dm1.com> wrote in message news:3F44F76A.5000409@dm1.com...don't even need to escape the backslashes, so:-- SNIP --If single-quoting (better, if you don't variable interpolation) youdon't even need to escape the backslashes, so:$pattern = '/\bsrc=([\'|"])[^\1]*[\1]/im';-- SNIP -- nope, not quite... Here's what I've got- $pattern='/\bsrc=([\'|"])([^\1])*[\1]/im'; the string that's coming in is- <IMG SRC="http://www.mydomain.net/templates/images/somepicture.gif" ALT="letter" BORDER="0" ALIGN="CENTER"> That's no newline or anything like that within the code (even with the 'm' option, i've had previous problems with that before), and I can simplify the expression and grab everything including the quotes, so I'm really not sure what's failing. Thanks- Thaddeus Ford, Mike [LSS] wrote:On 21 August 2003 17:03, Thaddeus J. Quintin wrote:Lets say I'm trying to extract some data from an HTML document. I want to get the values of the 'src' attributes of various tags. For example- <img src="http://www.yahoo.com"> here's the pattern I've been trying- /\bsrc=(['|"])[^\\1]*[\\1]/im Basically, match the 'src=' followed by some type of quote, followed by stuff that's not the quote originally matched, followed by the same quote. But, obviously, I can't run this, because the quotes in the pattern close off the string I'm trying to create. Are there ways of working with quotes?Just escape the quote like you've escaped the backslashes. If double-quoting: $pattern = "/\bsrc=(['|\"])[^\\1]*[\\1]/im"; If single-quoting (better, if you don't variable interpolation) you
$pattern = '/\bsrc=([\'|"])[^\1]*[\1]/im'; Cheers! Mike --------------------------------------------------------------------- Mike Ford, Electronic Information Services Adviser, Learning Support Services, Learning & Information Services, JG125, James Graham Building, Leeds Metropolitan University, Beckett Park, LEEDS, LS6 3QS, United Kingdom Email: m.ford@lmu.ac.ukTel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211