Jump to content

Searching string for integer length - help?!


musclehead

Recommended Posts

I need to search a string for a specific-size integer. For example, the string would be:

 

"This is my string 123456789 and it contains an integer."

 

In all my string values, the integers are all exactly 10 digits in length, and there are no other occurrences of this length integers. I would like to figure out a PHP function to search for any 10-digit-length integers in the string.

 

Any ideas? Thank you all!

Link to comment
Share on other sites

Aside from the point that in your example, the integer does not have 10 digits, you'll need to use regular expressions:

 

<?php
$str = 'This is my string 1234567890 and it contains an integer';
preg_match_all('|\d{10,10}|',$str,$numbers);
print_r($numbers);
?>

 

Edit: I Misread that. If this number only occurs once, and you just want to check for it's existance, then something like this would work:

 

<?php
$str = 'This is my string 1234567890 and it contains an integer';
if(preg_match('|\d{10,10}|',$str,$numbers)){
echo '10 digit number found! The number was: '.$numbers[0];
}else{
'No 10 digit number within this string!';
}

?>

Link to comment
Share on other sites

Thanks for the help...I failed to mention I need to replace those 10-digit (and sorry for only writing until 9...) integers with line breaks.

 

I tried:

 

$result = preg_replace("\d{10,10}","NEWENTRY",$string);

 

But that appears to be bailing as well.

 

I saw your edit post also and tried that, but it's only picking up one of the 10-digit integers:

 

String:

text text text 1231242523 text text text text text1313874376text text text text1232873298

 

The preg_match function is only detecting the first one, even within a loop.

Link to comment
Share on other sites

Sorry for all the posts - I've got it working now!

 

$newstr = preg_replace("|\d{10,10}|","<br />",$str);

 

That does the trick and replaces all occurrences of the integer w/ a line break. I was stuck on the formatting of the search string. Thanks very much for your help!!

Link to comment
Share on other sites

this does exactly what you asked for:

 

function MyFunction($str) {
  $newstr = '';
  $x = split(" ", $str);

  for ($i=0;$i < sizeof($x);$i++){
      if (strlen($x[$i]) == 9 && preg_match("/[0-9]/", $x[$i])) $x[$i]="New_Value"; 
      //Change "New_Value" to whatever you want to replace the 9 digit numbers with.
      $newstr .= $x[$i] . "\r\n";
  }
  return $newstr;
}

echo MyFunction("This is my string 123456789 and it contains an integer.");

 

edit: ok lol you done it yourself but i'll leave this post for other people..

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.