PHP Programmers Help Please: How to find a random string that starts with "Disp:" ?

The string im looking for will always be formatted this way---> DISP:123456789

The numbers following the : will be random.

How can I search text with php and pick out that random string every time?

3 Answers

Relevance
  • 8 years ago
    Best Answer

    You can search for a string in the form "DISP:123456789" using the function preg_match() which takes as arguments the regex pattern and the search string. For example:

    $text = 'some text that contains DISP:123456789';

    if ( preg_match( '/DISP:\d+/', $text ) ) {

    echo 'pattern found';

    }

    In this example, the search pattern is /DISP:\d+/ and the search text is stored in the variable $text. The pattern matches the text DISP:123456789 and preg_match() returns true.

    Also, preg_match() accepts a third argument that can be used to retrieve information about the text matched by the pattern. The example above can be more useful if the random number portion of the pattern is extracted and used in some way. For example:

    $text = 'some text that contains DISP:123456789';

    if ( preg_match( '/DISP:(\d+)/', $text, $match ) ) {

    echo 'the random number ' . $match[1] . ' was found';

    }

    In this example, preg_match() populates the $match array with the entire matched string at $match[0] and what was matched inside the first parenthetical expression, in this case (\d+), at $match[1]. The string output is:

    the random number 123456789 was found

    To make the pattern case insensitive so that it will match "DISP:123456789", "Disp:123456789" and "disp:123456789" the "i" modifier can be used. The pattern /DISP:(\d+)/ becomes /DISP:(\d+)/i

    More information on preg_match() can be found here:

    http://www.php.net/manual/en/function.preg-match.p...

  • 3 years ago

    as quickly as I first began this wacky interest surprisingly much precisely a 365 days in the past... I had no thought what "lol" or "lmao" or "Emo" or "OMG" meant...or maybe that a collection noted as "Disco Geeks For God" even existed... My thought became to return up with... GAFCYDWT...which stands for... (Get A Friggin Clue You damn Wussie Troll) regrettably for the community human beings cool human beings at great... It did no longer seize on...hee hee...

  • 8 years ago

    str_replace( $stringWithDISP, "", "DISP:" );

    This function will take the string with the "DISP:" and replace all occurrences with nothing.

Still have questions? Get your answers by asking now.