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
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";

}

?>

 

Link to comment
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";

}

?>

 

 

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";
}

Link to comment
Share on other sites

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'";

}

?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.