Jump to content

Get a Portion of a string


RabPHP

Recommended Posts

Greetings,

 

I have a variable that looks like "THISISSOME(2 EXTREMELY)RANDOMDATA12345AP" I need to strip out the random data and return everything after the first occurance of a number that is not inside a parenthesis.  In this example, I want to only get 12345AP.  The data may be random wordsand characters without a defined length.

 

This one has me baffled but I know it's possible, please advise!

 

Thanks

Rab

Link to comment
Share on other sites

s


<?php

$string = "THISISSOME(2 EXTREMELY)RANDOMDATA12345AP";
//get rid of everything before the ")"
$string = explode( ')', $string );
//Keep everything after the last ")"
$string = $string[count( $string ) - 1];
$strLen = strlen( $string );
$index = 0;
for ( $i = 0; $i < $strLen; $i++ )
{
if ( is_numeric($string[$i]) )
{

	//Grab where the numbers start
	$index = $i;
	//stop any more checks.
	$i = $strLen + 1;
}
}
//grab the string where the numbers start
$string = substr( $string, $index );
print $string;

// outputs 12345AP


?>

 

 

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.