1

So I'm kind of stuck on this - I'm looking to replace text in an array (easily done via str_replace), but I would also like to append text onto the end of that specific array. For example, my original array is:

Array (

[1] => DTSTART;VALUE=DATE:20130712
[2] => DTEND;VALUE=DATE:20130713
[3] => SUMMARY:Vern
[4] => UID:1fb5aa60-ff89-429e-80fd-ad157dc777b8
[5] => LAST-MODIFIED:20130711T010042Z
[6] => SEQUENCE:1374767972

)

I would like to search that array for ";VALUE=DATE" and replace it with nothing (""), but would also like to insert a text string 7 characters after each replace ("T000000"). So my resulting array would be:

Array (

[1] => DTSTART:20130712T000000
[2] => DTEND:20130713T000000
[3] => SUMMARY:Vern
[4] => UID:1fb5aa60-ff89-429e-80fd-ad157dc777b8
[5] => LAST-MODIFIED:20130711T010042Z
[6] => SEQUENCE:1374767972

)

Is something like this possible using combinations of str_replace, substr_replace, etc? I'm fairly new to PHP and would love if someone could point me in the right direction! Thanks much

1
  • Yes, I'm bringing iCal events into my site, and any "all day" events not in the "date-time" format trips it up. For example, these events are coming in as all-day "date" values, so I just need to convert them to date-time and it works much easier. It's a dirty way of doing it, but I just needed a quick solution, and can come back later and troubleshoot a bit further. Commented Jul 25, 2013 at 22:07

4 Answers 4

4

You can use preg_replace as an one-stop shop for this type of manipulation:

$array = preg_replace('/(.*);VALUE=DATE(.*)/', '$1$2T000000', $array);

The regular expression matches any string that contains ;VALUE=DATE and captures whatever precedes and follows it into capturing groups (referred to as $1 and $2 in the replacement pattern). It then replaces that string with $1 concatenated to $2 (effectively removing the search target) and appends "T000000" to the result.

Sign up to request clarification or add additional context in comments.

2 Comments

@EJK - If it worked for you, please click the check mark to the left of his answer to accept it.
Sorry, this is my first time using this site. But I must say, this is the fastest and best response I've ever had from a community site before - Thanks for the help!
1

The naive approach would be to loop over each element and check for ;VALUE=DATE. If it exists, remove it and append T000000.

foreach ($array as $key => $value) {
  if (strpos($value, ';VALUE=DATE') !== false) {
    $array[$key] = str_replace(";VALUE=DATE", "", $value) . "T000000";
  }
}

3 Comments

It will concat "T000000" to each position in the array, and no after each replace...
Oh, srry, I got confused your answer with the Bad Wolf's answer.
Lol. I spend a lot of minutes wrttting my answer, when I post it, had several similar answers. greetings!
0

You are correct str_replace() is the function that you are looking for. In addition you can use the concatenation operator . to append your string to the end of the new string. Is this what you are looking for?

$array[1] = str_replace(";VALUE=DATE", "", $array[1])."T000000";
$array[2] = str_replace(";VALUE=DATE", "", $array[2])."T000000";

1 Comment

- This is will append, even if it doesn't replace.
0
for($i=0;$i<count($array);$i++){
   if(strpos($array[$i], ";VALUE=DATE")){//look for the text into the string
      //Text found, let's replace and append
      $array[$i]=str_replace(";VALUE=DATE","",$array[$i]);
      $array[$i].="T000000";
   }
   else{
      //text not found in that position, will not replace
      //Do something
   }
}

If you want just to replace, just do it

$array=str_replace($array,";VALUE=DATE","");

And will replace all the text in all the array's positions...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.