0

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.

2
  • 1
    sadly i dont have time for a full answer, but look into regex Commented Sep 13, 2022 at 17:01
  • @seven_seas, yes that is probably one way. I used the maybe slow method as posted as answer which works. I don't need efficiency in this case so a couple of more milliseconds is ok :) Commented Sep 13, 2022 at 17:16

4 Answers 4

1

Regex is the easiest way:

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

echo $out[1];
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0
     //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

Comments

0

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

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.