0

At the moment I have this:

<?php
$stran = file_get_contents("http://meteo.arso.gov.si/uploads/probase/www/fproduct/text/sl/fcast_si_text.html");
$stran = str_replace("<h2>","\n",$stran);
$stran = str_replace("</h2>","\n",$stran);
$stran = str_replace("<h1>","\n",$stran);
$stran = str_replace("</h1>","\n",$stran);
$stran = strip_tags($stran);

echo $stran;
?>

Now this gives me some empty lines at the top. I also want to delete every text after "Vir: Državna meteorološka služba RS (meteo.si - ARSO)" including empty lines before this string.

I've tried some regular expressions but the all delete all text. Hot do I do it?

3
  • You can cut of a certain portion of the text by first determining the position of the text with strpos() and once you have that position you can slice the rest off with substr(). So something like $pos = strpos('ARSO)', $stran); $stran = substr($stran, $pos + 5); Commented Mar 17, 2016 at 16:58
  • Thanks, this works: $stran = substr($stran, 0, strpos($stran, "Vir:")); Commented Mar 17, 2016 at 16:59
  • I would check first if $pos !== false; because otherwise it empties the string when the ARPO) is not present in the string. Commented Mar 17, 2016 at 17:01

1 Answer 1

1

Can be done using regex.

// Convert h1/h2 opening/closing tags to new line, ignore case
$stran = preg_replace('/<\/?h[12]>/i', "\n", $stran);

$stran = strip_tags($stran);

// Remove all leading whitespace
$stran = preg_replace('/^\s+/', '', $stran);

// Remove everything after "Vir: ..."
$stran = preg_replace('/(?<=Vir: Državna meteorološka služba RS \(meteo.si - ARSO\)).*/s', '', $stran);    

Generally speaking I would recommend to really parse the html to extract the information. Have a look at http://php.net/manual/en/class.domdocument.php

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.