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]);
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