5

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

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Jun
  • 91
  • 1
  • 10
  • 2
    Use back ticks to encapsulate the code, file contents. You could use `strpos` with `substr`. – chris85 Mar 15 '17 at 23:25
  • did not know about strpos, thanks for your suggestion. I'll try it out! – Jun Mar 15 '17 at 23:29
  • @Jun please update the title and/or question to specify that the substring that you wish to remove does not occur more than once in the original string. For this reason, your question is not a duplicate of http://stackoverflow.com/questions/4517067/remove-a-string-from-the-beginning-of-a-string – mickmackusa Mar 16 '17 at 03:27
  • @Jun can you explain to me why you chose NishanthMatha's solution instead of mine which offers 7 different correct solutions, document references, and a demo? If it is about speed, his was corrected/edited after my post was last edited. I am confused by your decision. Please help future SO readers to find the *best answer* to your question -- that is what the green tick is meant to identify. – mickmackusa Mar 16 '17 at 19:40
  • @mickmackusa lol your comment made my day... for the first time I'm seeing someone compelling an OP to accept their ans ;) – Nishanth Matha Mar 16 '17 at 22:51
  • @NishanthMatha I have to ask the logic, because it doesn't make logical sense to me. Every question and answer on SO is meant to strive for greatness so that SO can be the best resource for researchers. – mickmackusa Mar 16 '17 at 22:54
  • @mickmackusa Oh it's not about efficiency for me at the moment because I'm still learning. His was just easier to understand and I was doing something similar already. I actually tried chris85's suggestion first (and it worked). So I feel like I need to pick any accepted answer. His was the closest thing I did. If it makes you feel better, I'll pick yours. I mean both way works. – Jun Mar 17 '17 at 13:36
  • Thanks Jun. I hope your question and my answer help many readers in the future. – mickmackusa Mar 17 '17 at 13:38

5 Answers5

3

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.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
  • The `str_replace()` method can easily be written as a one-liner: `$string = str_eplace(['date: ','start: '],'',$string,1);` – Doin Jun 20 '22 at 00:34
  • Thanks for that ping @Doin. In fact, the `count` parameter of `str_replace()` is NOT a "limiting" count, but a reference variable that contains the number of replacements that were made. That technique was actually demonstrating an untrue/bad approach. I have removed it. – mickmackusa Jun 20 '22 at 02:07
1

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

Sergei Kutanov
  • 825
  • 6
  • 13
  • The third parameter of `strstr()` defaults to false, so it can be omitted. – mickmackusa Mar 16 '17 at 04:37
  • This answer fails to trim the whitespace from the front of the new string and so doesn't meet the brief. – mickmackusa Mar 16 '17 at 04:46
  • No one said anything about the whitespace, but there you go. – Sergei Kutanov Mar 16 '17 at 15:16
  • Attention to detail. When you change `"date: march 27, 2017"` to `"march 27, 2017"` and `"start: 12:30pm"` to `"12:30pm"` you must remove the first word, the colon, and the space. Thanks for fixing this so that future SO readers can benefit from seeing a correct solution. – mickmackusa Mar 16 '17 at 19:28
1

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)
Nishanth Matha
  • 5,993
  • 2
  • 19
  • 28
0

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.

Ray Hunter
  • 15,137
  • 5
  • 53
  • 51
-1

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

Community
  • 1
  • 1
Louis Loudog Trottier
  • 1,367
  • 13
  • 26
  • This technique does not trim the substring from the front of the string. It also uses 3 function calls, where as there are at least 6 other ways to achieve the requested result with 2 or less function calls. – mickmackusa Mar 16 '17 at 02:49
  • ...just remembered a seventh method. – mickmackusa Mar 16 '17 at 04:27