How can I split this in PHP? - php

I am trying to split something in PHP, and I can't get it to work.. Been trying for a while now, so thought I would ask here.
So lets say that I have multiple <script> ... </script> in my source code, then what can I do to split these into a string. I'm trying with explode, but not working out as planned.
This is what I've tried so far:
$script = explode('<script>',$data,1);
echo htmlspecialchars($script[1]);
Tried that but it doesn't get any specific <script>.
Example script:
<script>
script here...
</script>
<script>
second script here...
</script>
So how will I go about getting the second script?
Sorry, I'm not the best at regex or parsing in PHP yet, and merry christmas to all of you! :)

Do not parse HTML with string functions. Or regex, for that matter. the <center> cannot hold regexes and HTML. But that's a different story. Instead, use an html parser, like Simple HTML DOM(Which, for some reason, is blocked by my high school's stupid firewall). Please correct me if I'm wrong, since I can't access the docs for it.
include("simple_html_dom.php");
$html=str_get_html($text);
$scripts=$html->find("script");
foreach($scripts as $script){
echo(htmlspecialchars($script));
}

Use loadHTML():
$doc = new DOMDocument();
// load the HTML string we want to strip
$doc->loadHTML($html);
// get all the script tags
$script_tags = $doc->getElementsByTagName('script');

Instead of string functions, I'd use a DOM Parser such as PHP's DOMDocument to extract the required data. Here's how you can do it:
$text = <<<TEXT
<script>
script here...
</script>
<script>
second script here...
</script>
TEXT;
$dom = new DOMDocument;
$dom->loadHTML($text);
echo $dom->getElementsByTagName('script')->item(1)->nodeValue;
Some explanation:
The text is loaded using loadHTML() method and then you use getElementsByTagName() method to get all the script tags. Now we use item(1) to specifically target the second <script> tag and then echo the nodeValue of that node.
Output:
second script here...

Related

Use PHP to echo whats inside div tags

I dont know what to research or where to start here.
What im trying to do is use PHP to read an HTML Page and pull out the raw text contained inside a div
the div is this
<div class="thingy">
test
</div>
When the php is executed, I want it to echo
Test
Is there an easy snippet for this, or can someone post a small script?
Edit: the html page with the Div is on another webpage.
What you're looking to do is parse HTML. Use the DOM module that comes with PHP to do this: http://php.net/manual/en/book.dom.php
You do NOT want to try to do this with regular expressions.
If you want to remove ALL the HTML tags from a document, use the PHP strip_tags() function: http://us3.php.net/strip_tags
While this could possibly be done using regex, I would recommend using a DOM parser. My reccommendation goes to SimpleHTML Dom Parser. Using it, here's how you would do what you want
$string = "<div class=\"thingy\">test</div>";
$html = str_get_html($string); // create the DOM object
$div = $html->find('div[class=thingy]', 0); // find the first div with a class of 'thingy'
echo $div->plaintext(); // echo the text contents
If you want to parse your html you can use it like
<?php
$str = '<div class="thingy">test</div>';
echo strip_tags($str);//OUTPUT : test
?>
As your html is on other webpage, start output buffering include that file in your main php script, do all manipulation on it to get the content.

How to clear all elements in a page with PHP?

Is there any way to clear all html elements in a php page?
For example I have 100 html elements in my page, is there anyway to remove them?
As we know with javascript we have innerHTML but in PHP what?
clear all html elements in a php page
That doesn't make sense. HTML elements only exist in the DOM after PHP has executed and sent an HTML document to the browser. Server-side, where PHP executes, there are no elements to remove.
If you're trying to manipulate the HTML you've already output, you need to capture it with output buffering (see ob_start, ob_get_contents and ob_end_clean) but if your goal is to "clear all html elements", presumably so you can output a different set of elements, you simply need to not output anything in the first case. If this sounds like what you're trying to accomplish, you need to look into simple conditional statements like if/else.
as we know with javascript we have innerHTML but in php what ?
There is no PHP-equivalent because PHP doesn't have access to the client-side DOM. It is purely a server-side technology, and the output of your PHP script is the input to the browser. The DOM and its elements are generated long after your PHP script has executed. If you have an XHTML fragment in a string, and you want to parse/manipulate it, you can use xpath.
If your question is "clear html elements in a php file", the answer is: strip_tags().
$string = '<p>hello</p>';
echo strip_tags($string);
Try this:
<?php
if(//why you want to clear the elements){
echo "<script language=\"javascript\">";
?>
//Append all elements in <div id="body">
var body = document.getElementById("body");
body.innerHTML ="";
<?php
echo "</script>";
#Output your new element
echo "New elements.";
}
?>
Try this, it should definitely work.
<?php
echo "<script>document.write('');</script>";
?>

PHP get external page content

i get the html from another site with file_get_contens, my question is how can i get a specific tag value?
let's say i have:
<div id="global"><p class="paragraph">1800</p></div>
how can i get paragraph's value? thanks
If the example is really that trivial you could just use a regular expression. For generic HTML parsing though, PHP has DOM support:
$dom = new domDocument();
$dom->loadHTML("<div id=\"global\"><p class=\"paragraph\">1800</p></div>");
echo $dom->getElementsByTagName('p')->item(0)->nodeValue;
You need to parse the HTML. There are several ways to do this, including using PHP's XML parsing functions.
However, if it is just a simple value (as you asked above) I would use the following simple code:
// your content
$contents='<div id="global"><p class="paragraph">1800</p></div>';
// define start and end position
$start='<div id="global"><p class="paragraph">';
$end='</p></div>';
// find the stuff
$contents=substr($contents,strpos($contents,$start)+strlen($start));
$contents=substr($contents,0,strpos($contents,$end));
// write output
echo $contents;
Best of luck!
Christian Sciberras
(tested and works)
$input = '<div id="global"><p class="paragraph">1800</p></div>';
$output = strip_tags($input);
preg_match_all('#paragraph">(.*?)<#is', $input, $output);
print_r($output);
Untested.

How do I insert HTML into a PHP DOM object? [duplicate]

This question already has answers here:
How to insert HTML to PHP DOMNode?
(5 answers)
Closed 7 years ago.
I am using PHP's DOM object to create HTML pages for my website. This works great for my head, however since I will be entering a lot of HTML into the body (not via DOM), I would think I would need to use DOM->createElement($bodyHTML) to add my HTML from my site to the DOM object.
However DOM->createElement seems to parse all HTML entities so my end result ended up displaying the HTML on the page and not the actual renders HTML.
I am currently using a hack to get this to work,
$body = $this->DOM
->createComment('DOM Glitch--><body>'.$bodyHTML."</body><!--Woot");
Which puts all my site code in a comment, which I bypass athe comment and manually add the <body> tags.
Currently this method works, but I believe there should be a more proper way of doing this. Ideally something like DOM->createElement() that will not parse any of the string.
I also tried using DOM->createDocumentFragment() However it does not like some of the string so it would error and not work (Along with take up extra CPU power to re-parse the body's HTML).
So, my question is, is there a better way of doing this other than using DOM->createComment()?
You use the DOMDocumentFragment objec to insert arbitrary HTML chunks into another document.
$dom = new DOMDocument();
#$dom->loadHTML($some_html_document); // # to suppress a bajillion parse errors
$frag = $dom->createDocumentFragment(); // create fragment
$frag->appendXML($some_other_html_snippet); // insert arbitary html into the fragment
$node = // some operations to find whatever node you want to insert the fragment into
$node->appendChild($frag); // stuff the fragment into the original tree
I FOUND THE SOLUTION but it's not a pure php solution, but works very well. A little hack for everybody who lost countless hours, like me, to fix this
$dom = new DomDocument;
// main object
$object = $dom->createElement('div');
// html attribute
$attr = $dom->createAttribute('value');
// ugly html string
$attr->value = "<div> this is a really html string ©</div><i></i> with all the © that XML hates!";
$object->appendChild($attr);
// jquery fix (or javascript as well)
$('div').html($(this).attr('value')); // and it works!
$('div').removeAttr('value'); // to clean-up
loadHTML works just fine.
<?php
$dom = new DOMDocument();
$dom->loadHTML("<font color='red'>Hey there mrlanrat!</font>");
echo $dom->saveHTML();
?>
which outputs Hey there mrlanrat! in red.
or
<?php
$dom = new DOMDocument();
$bodyHTML = "here is the body, a nice body I might add";
$dom->loadHTML("<body> " . $bodyHTML . " </body>");
// this would even work as well.
// $bodyHTML = "<body>here is the body, a nice body I might add</body>";
// $dom->loadHTML($bodyHTML);
echo $dom->saveHTML();
?>
Which outputs:
here is the body, a nice body I might add and inside of your HTML source code, its wrapped inside body tags.
I spent a lot of time working on Anthony Forloney's answer, But I cannot seem to get the html to append to the body without it erroring.
#Mark B: I have tried doing that, but as I said in the comments, it errored on my html.
I forgot to add the below, my solution:
I decided to make my html object much simpler and to allow me to do this by not using DOM and just use strings.

PHP dom - How to get the contents within a span tag that includes a hyperlink

I've done this before but can't find my code snippet.
I'd like to parse an html file and pull everything into my browser that sits between some span tags. There are other span tags in the html that I do not want so I figured I would limit the parsing to just the span tags that have the same css class. Can someone please give me an example of how to do this? Thanks.
$tags = $doc->getElementsByTagName('span');
This is a single row of the html I am trying to parse
<span class='close'>test row</span>
First attempt (untested):
$elts = $doc->getElementsByTagName('span');
foreach ($elts as $elt)
{
$className = $elt->getAttribute('class');
if (array_search('close', explode(' ', $className)))
{
// Do things with $elt since it matches.
}
}
In my opinion and experience it can be done by getElementsByTagName() ?? Just use some ajax-y function to call for it and you have your DOM element :)

Categories