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
https://forums.phpfreaks.com/topic/146046-get-a-portion-of-a-string/
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


?>

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.