Home > Software design >  file_get_contents to Extract Body using Get String
file_get_contents to Extract Body using Get String

Time:12-08

How to extract this to php with get string?

<Body>
"Example"
</body>

Then i want only the "Example"

Output Example with File get contents -> Get string function

CodePudding user response:

Try this:

<?php
$url = 'string with Example';
$string = file_get_contents($url);
$result = get_string('Example',$string);
echo $result;
?>

CodePudding user response:

You can use a regular expression to match <body> and </body> and capture the text in between:

<?php


// Using HEREDOC synatx for illustration
// $str = file_get_contents($file); should work as well

$str = <<<EOT
start<body>
Some text
Some more text
</body>
text after body
EOT;
$regex = "/.*<body>(.*?)<\/body>/ms";
$result = preg_match( $regex, $str, $matches);
print_r($matches[1]);

See https://3v4l.org/dl2hr

Note This is a very restricted use of a regular expression to extract data from HTML. You shouldn't treat this as a basic example for wider use. For that you should use DOMDocument

  •  Tags:  
  • php
  • Related