• Notice regarding image hosts and attachments. See here.

Blonde moment

Soldato
Joined
24 Nov 2002
Posts
16,376
Location
38.744281°N 104.846806°W
... I'm having one... partly as str_replace as no offset vars.

I have a string such as the following:

absfasdfasf--------------sf3ssafasf--------4asdfsdfasdfasdfasf.......

I want the position of the '3' (strpos) BUT, with all the hyphens before it removed:

absfasdfasfsf3ssafasf--------4asdfsdfasdfasdfasf.......

Likewise, for 4:

absfasdfasfsf3ssafasf4asdfsdfasdfasdfasf.......

I'm stuck :(
 
psyr33n said:
As in plug the figure (gained from using strpos() to locate 3's position) into substr(), I would guess.
But I need the position of the 3, not 3 itself using substr.

I think, I can just use str_replace as any - to the right of 3 are irrelevant to strpos.

Not sure.
 
Ah, I think I almost understand what you're doing - I thought you wanted the section of the string after strpos value, rather than the strpos() value itself.
jdickerson said:
I think, I can just use str_replace as any - to the right of 3 are irrelevant to strpos.
Yes, can you not just strip all the hyphens, and then run strpos() on the resulting string?

Code:
$string = str_replace('-', '', $string);
$needle = '3';
$pos = strpos($string, $needle);
 
Code:
<?php

$needle = '3';
$str = 'absfasdfasf--------------sf3ssafasf--------4asdfsdfasdfasdfasf.......';
$pos = strpos($str, $needle);

$prstr = str_replace('-', '', substr($str, 0, $pos));
$newstr = $prstr . substr($str, $pos);

?>
Like that? Slightly confuzzled logic (get position, split string, remove hyphens from first half, concatenate second half) but it gets the desired results.
 
It would be a hell of a lot easier if you just posted your end goal, instead of making multiple (now several) threads about manipulating the same string.
 
Back
Top Bottom