There are similar questions in SO, but I couldn't find any exactly like this. I need to remove everything up to (and including) a particular delimiter. For example, given the string File:MyFile.jpg
, I need to remove everything up to the :
, so that I'm left with only MyFile.jpg
. Thanks in advance!
Asked
Active
Viewed 8,399 times
5

Community
- 1
- 1

Sophivorus
- 3,008
- 3
- 33
- 43
-
@SubirKumarSao he's talking about PHP – Ejaz Apr 19 '13 at 12:38
-
I thought of `substr`, but the length of the substring before the delimiter may vary. I guess I need to combine it with `strpos`, but haven't figured the best way yet. – Sophivorus Apr 19 '13 at 12:38
-
Corrected. When I noticed the php tag. – Subir Kumar Sao Apr 19 '13 at 12:39
7 Answers
9
Use this preg_replace call:
$str = 'File:MyFile.jpg';
$repl = preg_replace('/^[^:]*:/', '', $str); // MyFile.jpg
OR else avoid regex and use explode like this:
$repl = explode(':', $str)[1]; // MyFile.jpg
EDIT: Use this way to avoid regex (if there can be more than one : in string):
$arr = explode(':', 'File:MyFile.jpg:foo:bar');
unset($arr[0]);
$repl = implode(':', $arr); // MyFile.jpg:foo:bar

anubhava
- 761,203
- 64
- 569
- 643
-
-
-
-
As to the `explode`, if there is more than one `:` it won't work, right? – Sophivorus Apr 19 '13 at 12:46
-
@FelipeSchenone: If there can be more than one `:` in the string then you can either use preg_replace or use my edited answer on explode. – anubhava Apr 19 '13 at 12:52
-
I wouldn't recommend using explode, as it causes more issues if there is more than one delimiter . – Ravindra Shekhawat Apr 19 '13 at 12:58
4
EDIT: this one works fine.
$str = "File:MyFile.jpg";
$str = substr( $str, ( $pos = strpos( $str, ':' ) ) === false ? 0 : $pos + 1 );

Sophivorus
- 3,008
- 3
- 33
- 43

Ravindra Shekhawat
- 4,275
- 1
- 19
- 26
-
2
-
1Beautiful answer, thanks. Could be simplified to `substr( $str, strpos( $str, ':' ) )` if one knew that the `:` _does_ appear in the string. – Sophivorus Apr 19 '13 at 13:13
-
3
Shorter codes:
To return everything BEFORE the FIRST occurence of a character, use strtok
. Example:
strtok(16#/en/go, '#')
will return16
To return everything AFTER the FIRST occurence of a character, use strstr
. Example:
strstr(16#/en/go, '#')
will return#/en/go
(Includes search character '#')substr(strstr(16#/en/go, '#'), 1)
will return/en/go
To return everything AFTER the LAST occurrence of a character, use strrchr
. Example:
strrchr(16#/en/go, '/')
will return/go
(Includes search character '/')substr(strrchr(16#/en/go/, '/'), 1)
will returngo

John Miller
- 527
- 4
- 15
1
You could use explode
to do this: link.
Something like:
$string = "File:MyFile.jpg";
list($protocol,$content) = explode(":", $string);
echo $content;

Glitch Desire
- 14,632
- 7
- 43
- 55
1
$str = "File:MyFile.jpg";
$position = strpos($str, ':');//get position of ':'
$filename= substr($str, $position+1);//get substring after this position

Aris
- 4,643
- 1
- 41
- 38
-
1Yes, this should be the accepted answer ... most sensible, brief and efficient. Can be shortened as: `substr($str, strrpos($str, ":")+1);` – davea0511 Apr 16 '15 at 21:35
-
...but this fails if the delimiter is not found ...which is why is should NOT be the accepted answer. – mickmackusa Jun 29 '21 at 04:34
0
Two simple ways:
$filename = str_replace('File:', '', 'File:MyFile.jpg');
or
$filename = explode(':', 'File:MyFile.jpg');
$filename = $filename[1];

pmayer
- 341
- 2
- 13
-
The first one will work for the example, but not in the general case. The second one seems like an answer! – Sophivorus Apr 19 '13 at 12:40
0
Sample String:
$string = 'value:90|custom:hey I am custom message|subtitute:array';
convert string to array
$var = explode('|', $string);
Check Results:
Array(
[0] => value:90
[1] => custom:hey I am custom message
[2] => subtitute:array)
Declare an array variable
$pipe = array();
Loop through string array $var
foreach( $var as $key => $value ) {
// get position of colon
$position = strrpos( $value, ':' );
// get the key
$key = substr( $value, 0, $position );
//get the value
$value = substr( $value, $position + 1 );
$pipe[$key] = $value; }
Final Result:
Array(
[value] => 90
[custom] => hey I am custom message
[subtitute] => array)

JAY
- 21
- 3