1

I'm trying to check if a variable has a number that starts with >> i.e., >>12345 and then separate that number into a different variable.

For example:

$my_string = "
>>12345

Hello this is an example string.
";

I'd like to store the '>>12345' in the database as a separate variable. Similar to image boards.

1

1 Answer 1

3

This is easily done with a regular expression:

<?php

$my_string = "
>>12345

Hello this is an example string.
";
preg_match("/(>>\d+)/", $my_string, $matches);
echo $matches[1];

The Regex looks for >>followed by any number of numeric digits, then captures that group into the $matches array.

Demo: https://3v4l.org/cKE6J

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.