Extract a substring from a string using PHP (Cake PHP application)
I need help to solve the following problem.
I have a input string as follows:-
<span class="notranslate">msgid "Default_TOP_01" </span> <br> <span class="notranslate">msgstr </span>"home <stro开发者_开发技巧ng>page </strong>" <br> <span class="notranslate">msgid "Default_TOP_02" </span> <br> <span class="notranslate">msgstr </span>"content <span>within </span>"
The desired output should be:-
msgid "Default_TOP_01" \n msgstr "home <strong>page </strong>" \n msgid "Default_TOP_02" \n msgstr "content <span>within </span>"
I need the starting and ending span tags with class="notranslate" to be removed. The <br> tags should be replaced by \n. Any tags withing the "msgstr" value should not be removed, for example msgstr "content <span>within </span>"
First run str_replace on the string to replace any occurence of <br>
with \n
and then strip all HTML tags from the string with strip_tags.
<?php
$str = '<span class="notranslate">msgid "Default_TOP_01" <br> <span class="notranslate">msgstr </span>"home <strong>page </strong>" <br> <span class="notranslate">msgid "Default_TOP_02" </span> <br> <span class="notranslate">msgstr </span>"content <span>within </span>"';
echo 'Original: '.$str;
echo 'Modified: '.strip_tags(str_replace('<br>', "\n", $str));
?>
精彩评论