php - Finding content of tags, removing tags and outputting it into a variable -
im looking way to:
- extracting piece of text variable
- removing tags of kind extracted text
- outputting result without tag. (example below)
for example, let's $string:
placeholder text placeholder text placeholder text placeholder text <tag1> <tag2> lorem ipsum <tag2> dolor sit amet </tag1> placeholder text placeholder text placeholder text placeholder text
i want extract contents within <tag1>
, remove <tag2>
's , output text in string, replacing first example looks so:
placeholder text placeholder text placeholder text placeholder text <tag1> lorem ipsum dolor sit amet </tag1> placeholder text placeholder text placeholder text placeholder text
i have tried using preg_replace()
:
preg_match("/<tag1>(.*?)<\/tag1>/i", $string, $matches); foreach($matches $value){ $code = str_replace("<tag2>", "", $value); $string = str_replace($value, $code, $string); }
but doesn't work reason
may need this:
$string = " placeholder text placeholder text placeholder text placeholder text <tag1> <tag2> lorem ipsum <tag2> dolor sit amet </tag1> placeholder text placeholder text placeholder text placeholder text" ; preg_match("/<tag1>([^\"]*)<\/tag1>/i", $string, $matches); $formatted_part = preg_replace("/((<tag2>|<\/tag2>)[\s\t]*[\r\n]+)/", "", $matches[1]); $new = str_replace($matches[1], $formatted_part, $string); var_dump($new);
Comments
Post a Comment