I have a question about how to manipulate php string.
For example, I have a text string:
$text="
---<updated:12/20/06>---
this is 2nd entry
---<updated:12/19/06>---
this is 1st entry
";

Now if I want to remove "---<updated:XX/XX/XX>---" and only display:
this is 2nd entry
this is 1st entry

how can I do that? Is there a php function can do this?

Thanks

:confused:

I have a question about how to manipulate php string.
For example, I have a text string:
$text="
---<updated:12/20/06>---
this is 2nd entry
---<updated:12/19/06>---
this is 1st entry
";

Now if I want to remove "---<updated:XX/XX/XX>---" and only display:
this is 2nd entry
this is 1st entry

how can I do that? Is there a php function can do this?

Thanks

:confused:

Hi there,

Where do you get the string values from? Is it from a database or not? Are the values divided by some kind of character - e.g. break, new line, space or any other character - I see the values are listed on several lines, so is there a line break after each value?

In general - there are many ways to do this, however you have to find a way to separate the ---<...>--- part from the rest.

One way is to find a separating character and explode the string into an array, where each part is a value of the array.

If no separating character is available, you can trim XX number of characters from the beginning or end of each string, but you will have to run through the script several times.
Answer the questions and we'll figure it out.

Hi there

I'm new at this so I hope it comes through OK.

You could use the ereg_replace function. I haven't used it a lot but I tested the following and it worked. The pattern string can probably be improved but this is a start. It assumes that the text you want to remove is just as your example showed (the dates can vary).

[B]$mystr1[/B]=[I]"Some text.---<updated:12/20/06>--- More text---<updated:11/19/06>---"[/I];
[B]$mystr2[/B]=[B]ereg_replace[/B]([I]"(---<updated:)[0-9]{2}(/)[0-9]{2}(/)[0-9]{2}(>---)"[/I],[I]""[/I],[B]$mystr1[/B]);

Hope this helps


Damien

This article has been dead for over six months. Start a new discussion instead.