Re: Search for string followed by whitespace
| From: | Daniel Brown | Date: | Mon, 19 Sep 2011 14:22:48 +0000 |
| Subject: | Re: Search for string followed by whitespace | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-314938@lists.php.net to get a copy of this message | ||
On Sun, Sep 18, 2011 at 15:12, Tim Streater <tim@clothears.org.uk> wrote:
> At the moment, I'm doing this:
>
> $start = stripos ($body, "<a ", $loc);
>
> You'll note the space after the '<a'. But I really need to search in $body
> for '<a' followed by any whitespace char, at least one, starting at the $loc'th
> character, and returning the location of the string in $start.
>
> I had a look at the PCRE and POSIX regexp functions to no avail. Is there a slick way of doing
> this with one function call or should I just search for '<a' and brute-force check that
> the next char is ' ' or '\t' or '\n'?
>
> Thanks,
Try something along this line:
<?php preg_match_all('/<a\b/Ui',$text,$matches); ?>
Replace $text with the text through which you want to look, and
your results will be an array in $matches. The \b switch in the
regexp will match any word/letter border, which includes whitespaces,
tabs, newlines, periods, et cetera. Essentially, anything that you'll
want to match inside HTML tags, be they valid or otherwise. The U
modifier makes the search "Ungreedy," while the i modifier makes it
case-iNsEnSiTiVe.
--
</Daniel P. Brown>
Network Infrastructure Manager
http://www.php.net/