Re: Searchingfor text within a string
| From: | Monte Ohrt | Date: | Tue, 29 Aug 2000 14:31:55 +0000 |
| Subject: | Re: Searchingfor text within a string | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-14130@lists.php.net to get a copy of this message | ||
How flexible does it need to be?
If you just want to search for "100%", then use strstr().
if(strstr($string,"100%"))
// display red light
If you want to display different lights for different
percentages, you may want to pull out the percentage
first. This will match a percentage preceeded and followed
by non-word characters, capturing the percentage amount:
if(preg_match("/\b([\d]+)%\b/",$string,$match))
// $match[] contains your percentage amt
Patrick Brown wrote:
>
> I'm trying to search for a piece of text within a long string. It is for a
> basic ping utility that will just provide a green or red light.
>
> An example string is:
>
> PING 1.1.1.1 (1.1.1.1): 56 data bytes --- 1.1.1.1 ping statistics --- 1
> packets transmitted, 0 packets received, 100% packet loss
> (all on one line)
>
> This is the result of the following command:
>
> passthru('ping -c 1 2.1.1.1', $ping);
> echo $ping;
>
> I need to look for the '100%' part of the string and assign it as a
> variable.
>
> I'll do something like:
>
> if ($loss = '100%'){
> display a red light;
> }
> else {
> display a green light;
> }
>
> Thanks,
>
> Pat
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
> For additional commands, e-mail: php-general-help@lists.php.net
> To contact the list administrators, e-mail: php-list-admin@lists.php.net
--
Monte Ohrt <monte@ispi.net>
http://www.ispi.net/