In PHP, how do I search through a large string to see if it contains a smaller string?
June 12, 2007 11:09 AM Subscribe
In PHP, how do I search through a large string to see if it contains a smaller string?
In PHP, I am opening a html file and reading in one line at a time. The problem is that I want to seach through the line to find a specific part.
For example.... I read in the following line into a variable called "$theData"
" >td height="19" bgcolor="#FFFFFF" width="140"<>font face="Arial, Helvetica" size=2<>a href="GenAvailGraph.php?id=510000054" target="_self"<5 10000054>/td<"
Now I want the search for "510000054" in $theData and return true or false depending on in the string has "510000054" contained in it.
Does anyone have any ideas on how to do this?>>5>
In PHP, I am opening a html file and reading in one line at a time. The problem is that I want to seach through the line to find a specific part.
For example.... I read in the following line into a variable called "$theData"
" >td height="19" bgcolor="#FFFFFF" width="140"<>font face="Arial, Helvetica" size=2<>a href="GenAvailGraph.php?id=510000054" target="_self"<5 10000054>/td<"
Now I want the search for "510000054" in $theData and return true or false depending on in the string has "510000054" contained in it.
Does anyone have any ideas on how to do this?>>5>
$pattern = "510000054";
if (stristr($theData, $pattern)) { echo "Found it!"; return true; } else return false;
posted by Blazecock Pileon at 11:17 AM on June 12, 2007
And to get a boolean use the super equality operator (
posted by Khalad at 11:19 AM on June 12, 2007
===
or !==
), as in: strpos(...) !== false
.posted by Khalad at 11:19 AM on June 12, 2007
if ( strpos( $haystack , $needle ) !== false )
$needle_found_in_haystack = true;
else
$needle_found_in_haystack = false;
posted by scottreynen at 11:19 AM on June 12, 2007
$needle_found_in_haystack = true;
else
$needle_found_in_haystack = false;
posted by scottreynen at 11:19 AM on June 12, 2007
Regex.
Manual page.
posted by IronLizard at 11:23 AM on June 12, 2007
Manual page.
< ?phpbr>
// The "i" after the pattern delimiter indicates a case-insensitive search
if (preg_match("/php/i", "PHP is the web scripting language of choice.")) {
echo "A match was found.";
} else {
echo "A match was not found.";
}
?>
>
posted by IronLizard at 11:23 AM on June 12, 2007
Regex would work, but the OP just wanted to search for a substring within a string, in which case the strpos/stripos/strstr/etc etc functions are a better choice (higher performance)
From the PHP manual:
Do not use preg_match() if you only want to check if one string is contained in another string. Use strpos() or strstr() instead as they will be faster.
posted by mebibyte at 11:37 AM on June 12, 2007
From the PHP manual:
Do not use preg_match() if you only want to check if one string is contained in another string. Use strpos() or strstr() instead as they will be faster.
posted by mebibyte at 11:37 AM on June 12, 2007
I noticed that right after posting. ;)
posted by IronLizard at 11:42 AM on June 12, 2007
posted by IronLizard at 11:42 AM on June 12, 2007
I'd say regexes are a safer way to go, looking at the OP's example, since you can require an exact word match ("\b510000054\b"). I doubt the OP wants to match "5100000540" when looking for "510000054". But only the OP knows for sure what the range of inputs is and whether this really matters.
posted by ldenneau at 12:03 PM on June 12, 2007
posted by ldenneau at 12:03 PM on June 12, 2007
I'd highly recommend spending some quality time with the PHP string functions manual page.
posted by treepour at 12:20 PM on June 12, 2007
posted by treepour at 12:20 PM on June 12, 2007
This thread is closed to new comments.
$pos = strpos($theData, "510000054");
posted by bshort at 11:14 AM on June 12, 2007