John Davidson

xml - Complicated text replace per line in PHP string

0 comments
Message:


I have a string that contains lines like the following


<category id blabla>2</category>
...
<name>bloblo</name>
<category id blabla>35</category>

and an array like this


[2] => Shoes
[35] => T-Shirts

I want to scan every line that contains the word "category", putting the number between tags in a variable, so i can map it with the array and then replace the number with the name of the category.


I am trying to find an effective way to do this, as the string contains almost 12k lines.
Tried with preg_split in foreach, but i am afraid the script crashes and i get no output (will try again tomorrow).
Puting the XML code in simplexml_load_string neither seems to work (it works with smaller strings, but not with this one).



My Answer:

Tove
Jani
Reminder
Don't forget me this weekend!

I need to replace the text inside the tags with a new text, but the replacement text is complicated and needs to be generated dynamically for each line.
For example, I need to replace "Don't forget me this weekend!" with "Reminder for Tove: Don't forget to buy milk and eggs."
How can I achieve this in PHP?

Rate this post

4 of 5 based on 2924 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us