Home > Software design >  Get word from locating "subword" in string
Get word from locating "subword" in string

Time:09-14

I have a string with a link looking like this:

$message = 'Text before link ... www.website.com/verify/12345/ ... text after link.';

After doing a if(str_contains($message, 'verify')) I now want to find the number 12345. The number can be any other number and can be of different length.

One way I could think of is to somehow get the complete link, explode with '/', and then the numbers should be the last or next to last part of the resulting array.

  1. Is there a better way of finding the number?
  2. If not, how can I retrieve the complete link from the string to then explode it?

For point two I first thought of exploding the entire string with ' ' (space) and then loop through until I find one containing 'verify', but this can be inefficient since the message can be very long. Then I though of using substr($message, mb_strpos($message, 'verify') 7, $length_of_number); but since the number can vary in length I don't know what to put for $length_of_number.

CodePudding user response:

Regex is the easiest way:

preg_match('/verify\/(d )/i', $message, $out);

echo $out[1];

CodePudding user response:

For anyone wanting an answer, here is a solution without regex:

$message = 'Text before link ... www.website.com/verify/12345/ ... text after link.';
$exploded_message = explode(' ', $message);
$verify_link = '';
foreach ($exploded_message as &$substring) {
    if(mb_strpos($substring, 'verify') !== false){
        $verify_link = $substring;
        break;
    }
}

$exploded_link = explode('/', $verify_link);
$user_id = $exploded_link[count($exploded_link)-2];

CodePudding user response:

     //string to parse.
     $message = 'Text before link ... www.website.com/verify/12345/ ... text after link.';

   if (str_contains($message, 'verify')) {
        //start word.
        $start_word = 'verify/';
        //end word.
        $end_word = '/';
     
        //Find the starting position of your start word.
        $subtring_start = strpos($message, $start_word);
        //Use the start words length to get the end index.
        $subtring_start  = strlen($start_word); 
        //Build the length for the substring in the next step.
        $substring_size = strpos($message, $end_word, $subtring_start) - $subtring_start; 
        //Create word using index substring_start of length substring_size.
        $final_word = substr($message, $subtring_start, $substring_size); 
     
         //output your final word.
         echo $final_word; 
    } else {
         echo "Verify Not Found";  
    }

Output:

12345

CodePudding user response:

Please try below code.. Its will solve your problem...

 <?php
    $message = 'Text before link ... www.website.com/verify/12345/ ... text after link.';
    
    $beforeStr = 'verify/';
    
    $user_id = substr($message, strpos($message, $beforeStr)   strlen($beforeStr), strrpos($message, '/')-(strpos($message, $beforeStr)   strlen($beforeStr)));
echo $user_id;
    ?>
  •  Tags:  
  • php
  • Related