php - Finding content of tags, removing tags and outputting it into a variable -


im looking way to:

  1. extracting piece of text variable
  2. removing tags of kind extracted text
  3. 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

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -