1

I'have a text composed of many strings and for each string, if the check is true, I would like to get a specific one having some attributes:

  1. the string starts with a sign like "atm"
  2. after the sign there is a numeric part with a variable lenght

i.e. the word could be like atm123456 or atm7890

Any help is appreciated, thanks.

5
  • 2
    Show us some of your code, what you have tried, etc. Commented Aug 28, 2014 at 13:28
  • i have not done anything yet, i can't figure out the solution.. Commented Aug 28, 2014 at 13:31
  • something like substr($text, $start-point, $lenght) but how to calculate the lenght of the numeric part only? Commented Aug 28, 2014 at 13:32
  • @Luke substr is also a good way to do yes, to calculate the length of the numeric part you could just read from 4 to the end of the string. Commented Aug 28, 2014 at 13:43
  • @ClémentMalet thank you, the problem is that the numeric part has a variable lenght and after the numeric part there is a space and then another string Commented Aug 28, 2014 at 13:46

3 Answers 3

1

You can use regular expression and preg_match() function

$string = "atm123456";
$pattern = "(atm\d+)";
preg_match($pattern, $string, $matches); // you may use preg_match_all() as well
print_r($matches);

Output:

Array
(
    [0] => atm123456
)

PHP demo | Regex demo

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

Comments

1

Can you try this

    $thestring='atm123456';
    $thestrToEx = explode(' ',$thestring );
    $thestrToExArr=array_walk($thestrToEx,'intval');
    $theValues=explode($thestrToExArr,$thestring);
    echo $thestrToExArr.$theValues[1];

Comments

1

in case of multiple value or repeated items. you can use preg_match_all like this

$string = "atm123456 with atm7890 items";
$pattern = "(atm\d+)";
preg_match_all($pattern, $string, $matches);
print_r($matches);

3 Comments

sir, I just give the option in case of preg_match_all with string example, you were just considering preg_match only.
it's like the solution by hex494D49 btw it's ok
It was already mentioned in my answer; even in my regex demo. I'm ok with that, just... weird to me man. Well, have fun ;)

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.