Jump to content

[SOLVED] Search a string of text...


Teck

Recommended Posts

I would like to search a string of text for "x" if "x" is found then it echo's something, else it echo's something else... Anyways I'm kind of lost as to the best way to search a string of text (for example "1,2,3,4,5,6,7,8,9,10,11,12") and find lets say "7,"...

 

If someone could point me in the right direction that would be awesome!  ;D

Link to comment
https://forums.phpfreaks.com/topic/73609-solved-search-a-string-of-text/
Share on other sites

<?php

$mystring = '1,2,3,4,5,6,7,8,9,10,11,12';

$findme  = '7';

$pos = strpos($mystring, $findme);

 

// Note our use of ===.  Simply == would not work as expected

// if the '7' was the 0th (first) character.

if ($pos === false) {

    echo "The string '$findme' was not found in the string '$mystring'";

} else {

    echo "The string '$findme' was found in the string '$mystring'";

    echo " and exists at position $pos";

}

?>

 

<?php

$mystring = '1,2,3,4,5,6,7,8,9,10,11,12';

$findme  = '7';

$pos = strpos($mystring, $findme);

 

// Note our use of ===.  Simply == would not work as expected

// if the '7' was the 0th (first) character.

if ($pos === false) {

    echo "The string '$findme' was not found in the string '$mystring'";

} else {

    echo "The string '$findme' was found in the string '$mystring'";

    echo " and exists at position $pos";

}

?>

 

 

Just a quick question... could i do something like:

 

$mystring = '1,2,3,4,5,6,7,8,9,10,11,12';
$findme   = '43';
$findme2   = '61';
$findme3  = '7';
$pos = strpos($mystring, $findme || $mystring, $findme2 || $mystring, $findme3);

if ($pos === false) {
    echo "The string '$findme' was not found in the string '$mystring'";
} else {
    echo "The string '$findme' was found in the string '$mystring'";
    echo " and exists at position $pos";
}

if u want to search multiple string from mystring, i suggest u use preg_match() ....

 

<?php

$mystring = '1,2,3,4,5,6,7,8,9,10,11,12';

$findme  = '7';

$findme1  = '43';

$findme2  = '61';

$pos = strpos($mystring, $findme);

 

if (preg_match("/$findme|$findme1|$findme2/", $mystring)) {

    echo "Found in the string '$mystring'";

} else {

    echo "Not found in the string '$mystring'";

}

?>

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.