Link to home
Create AccountLog in
Avatar of psimation
psimationFlag for South Africa

asked on

Deleting an array element in a string

Hi

I'm trying to do detect the first ocurrance of a Character/phrase in a string with strpos:

$string = "ad3432XabcdefgXadasdfXsdfsdfsdfY";
$pos = strpos($string,"X");

Then, I want to DELETE all the characters in the string up untill $pos, so that the character I'm looking for is now $string[0];

ASKER CERTIFIED SOLUTION
Avatar of hernst42
hernst42
Flag of Germany image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of psimation

ASKER

Hi hernst

Thanks, that seems to be working OK -

Can you just help me with one last thing:

in my example above,
$string = "ad3432XabcdefgXadasdfXsdfsdfsdfY";

Let's say it is:
$string = "ad3432XabcdefgXadasdfXsdfsdfsdfY123456787";

With your help, after applying the code, $string will now look like this:
$string = "XabcdefgXadasdfXsdfsdfsdfY123456787";

Now, I'm trying to chop the last bit off including Y...so that it looks like this only:
$string = "XabcdefgXadasdfXsdfsdfsdf";


You might try this. Maye the -1 is too much, or to less
if (strpos($string, 'X') !== false) {
   $string = substr($string, strpos($string, 'X'));
}
if (strpos($string, 'Y') !== false) {
   $string = substr($string, 0, strpos($string, 'Y')-1);
}

Open in new window