Jun
Jun

Reputation: 91

PHP - How to splice a string at a certain character?

I want to make a function that grabs each line from a file after the colon. I'm having trouble slicing the string at said character.

So I want to slice this:

"date: march 27, 2017" to "march 27, 2017" "start: 12:30pm" to "12:30pm" ...etc.

Note: I don't need help writing the actual function, I just want to know how to splice the line at the first colon

Upvotes: 5

Views: 11247

Answers (5)

mickmackusa
mickmackusa

Reputation: 48073

For your case, using: $string = "date: march 27, 2017"; or $string = "start: 12:30pm";

You can select any one of these techniques:

*note: If there are concerns about the existence of the needle (colon or colon space) then you should employ one of the options that is false-proof, otherwise additional considerations will be necessary to catch strings without the needle.

Use strpos() & substr() *false-proof:

$string = ($pos = strpos($string, ": ")) ? substr($string, $pos + 2) : $string;

Use strstr() & substr() *false-proof:

$string = ($sub = strstr($string, ": ")) ? substr($sub, 2) : $string;

Use explode() *requires colon space to exist:

$string = explode(': ', $string, 2)[1];

Use explode() & end() *no longer a one-liner, but false-proof:

$array = explode(': ', $string, 2);
$string = end($array);
// nesting explode() inside end() will yield the following notice:
// NOTICE Only variables should be passed by reference

Use preg_replace() with a regex pattern *false-proof:

$string = preg_replace("/^.*?:\s/", "", $string);

Use preg_match() with a regex pattern not a one-liner, but false-proof:

$string = preg_match("/^.*?:\s\K.*/", $string, $m) ? $m[0]: $string;

Here is a Demo for anyone who might want to run some tests on their own snowflake case.

Upvotes: 3

Sergei Kutanov
Sergei Kutanov

Reputation: 825

Use strstr - http://php.net/manual/en/function.strstr.php

Should like something close to

$str = 'date: march 27, 2017';
$str = strstr($str, ':');
$str = trim(substr($str, 1));

var_dump($str);
string(14) "march 27, 2017"

Haven't tested it but according to the documentation it should do the trick

Upvotes: 1

Nishanth Matha
Nishanth Matha

Reputation: 6081

As @chris85 suggested, a solution using strpos and substr:

$date = "date: march 27, 2017";
$yourString = $date;
//get the position of `:`
if(strpos($date, ":")!==false) {
    //get substring    
    $yourString = substr($date, strpos($date, ":") + 1);    
}
echo $yourString;

EDIT

As per @mickmackusa comment, the above answer may has trailing spaces before the extracted text to get over it you can use:

$yourString = ltrim($yourString)

Upvotes: 1

Louis Loudog Trottier
Louis Loudog Trottier

Reputation: 1397

You could explode it at the : http://php.net/manual/en/function.explode.php

Then splice the array http://php.net/array_splice

And restore the string with implode http://php.net/manual/en/function.implode.php

Also see :How to splice an array to insert array at specific position?

$x=explode(':',$string);
array_splice($x, 1, 0, ['text_Added']);
$string = implode($x);

EDIT:

If you just want to remove date: from the string you can trim it with the 2nd argument as filter.http://php.net/manual/en/function.trim.php

echo trim('date: march 27, 2017" to "march 27, 2017" "start: 12:30pm" to "12:30pm','date:');

//march 27, 2017" to "march 27, 2017" "start: 12:30pm" to "12:30pm

Or even just str_replace it. with the 4th argument, 1 to stop after the 1rst replace http://php.net/manual/en/function.str-replace.php

Upvotes: -1

Ray Hunter
Ray Hunter

Reputation: 15557

Here is a simple solution:

var_dump(explode(':', "date: march 27, 2017", 2));

That will output the following:

array(2) {
  [0]=>
  string(4) "date"
  [1]=>
  string(15) " march 27, 2017"
}

Then you have the value in the [1] index and you have the start in the [0] index. This will allow you to perform additional logic if needed.

You can then call trim() on the value to remove any white space.

Upvotes: 0

Related Questions