Nadia Krawczyk
Nadia Krawczyk

Reputation: 119

A PHP substring issue

I got a simple php issue which is that I don't know how to use substring here...

ABCDEF[RAND NUMBER 1-10000000]GH

And so I need to get that random number using substring,

I dont know if my brain works correctly today but I really couldnt figure how to do that.

Upvotes: 0

Views: 119

Answers (3)

mpapec
mpapec

Reputation: 50667

Regular expressions are better when you don't know where in the text is desired substring,

$string = "ABCDEF10000000GH";

if (preg_match("/(\d+)/", $string, $m)) {
  print $m[1];
}

Upvotes: 3

Pankaj Garg
Pankaj Garg

Reputation: 1322

Regex is always better in this case but If your string pattern is fixed like ABCDEF123456GH, you can simply use substr like

$str = "ABCDEF432465GH";
echo substr($str, 6, -2);

Upvotes: 3

CodeBird
CodeBird

Reputation: 3858

This will do, no need for substr or anything:

$string='ABCDEF432465GH';
echo preg_replace('/[a-z\[\] ]*/i', '', $string);

https://eval.in/136918

Upvotes: 3

Related Questions