How to find content using PHP regular expression - php

I want to find content inside of a DIV. I just want to specify the DIV’s id; the rest is automatically adjusted under preg_match.
E.g.:
<div id="midbuttonarea" etc...>some text...</div>

Brenton is right. Anyway, here you have:
<?php
function findDivInnerHtml($html, $id){
preg_match("/<div .* id=\"{$id}\" .*>(.*)<\\/div>/i", $html, $matches);
return $matches[1];
}
?>
Sample usage:
<?php
$html = '<div id="other"> xxx </div> <div id="midbuttonarea" etc...>some text...</div> ';
$innerHTML = findDivInnerHtml($html, 'midbuttonarea');
echo $innerHTML; //outputs "some text..."
?>
Hope this helps.

You're much more likely to get a good result using an HTML parser to parse HTML instead of Regex. It's extremely difficult to parse HTML with Regex, and the result may not be very reliable.
Check the accepted answer to this question: How do you parse and process HTML/XML in PHP? for some suggestions on how to go about it.

Related

Echoing un-escaped HTML

Very unusual question.
I came across some code some years in the pass that was including conditions and normal PHP syntax while echoing all content.
My question is how is that Technic/syntax called. I have been googling with some very broad terms and can't find what im looking for.
If my memory is correct, The code I viewed long time ago had un-escaped HTML and it was not required to start and stop PHP processing with <?php ?>
I Have a method within a class called Template\Labels::User()
the only purpose of that method is to echo the proper html to create a label within my webapp so that the pages are lighten of code and clear to anyone viewing the code.
Id like to avoid, having to <?php ?> for very simple boolean if
Any one know what I am looking for ?
static function User($UserObj,$isLink = true){
?>
<div class="image label bg-purple" style="margin: 4px;">
<?php if($isLink){
?><a href=""><?php
} ?>
<img src="<?php echo $UserObj -> ProfilePicture; ?>" style="height: 2em;" class="img-circle" alt="User Image">
<label style="font-size: 90%"><?php echo $UserObj->FirstName{0}.$UserObj->LastName{0}; ?></label>
<?php if($isLink){
?></a><?php
} ?>
</div>
<?php
}
Edited
After some more research by going through PHP documentation on Operator
I found Nowdoc string quoting
Can someone shed some light onto Nowdocs are to single-quoted strings what heredocs are to double-quoted strings. A nowdoc is specified similarly to a heredoc, but no parsing is done inside a nowdoc. The construct is ideal for embedding PHP code or other large blocks of text without the need for escaping. It shares some features in common with the SGML
http://php.net/manual/en/language.types.string.php#language.types.string.syntax.nowdoc
Its good that you added code to your question so that we can all see what you are dealing with here. Now to me what I understand with your question is that you want to avoid using php tags to echo some html code based on if condition.
<?php
static function User($UserObj,$isLink = true){
$html = '<div class="image label bg-purple" style="margin: 4px;">';
if($isLink) $html .= '<a href="">';
$html .= '<img src="'.#$UserObj->ProfilePicture.'" style="height: 2em;" class="img-circle" alt="User Image">';
$html .= '<label style="font-size: 90%">'.#$UserObj->FirstName[0].#$UserObj->LastName[0].'</label>';
if($isLink) $html .= '</a>';
echo $html;
}
?>
In my thinking I thought you should just have to run php tags once and use a simple variable to add your html code to so that you can print at the end of the function.
I didn't understand some of your images but all the same your issue is printing unescaped html in PHP. In other words you want to have raw html.
There are two functions am thinking of right now which you can use depending on your desired output: html_entity_decode() and htmlentities().
html_entity_decode() is the opposite of htmlentities() in that it converts all HTML entities in the string to their applicable characters.
<?php $orig = "I'll \"walk\" the <b>d
$a = htmlentities($orig);
$b = html_entity_decode($a);
echo $a; // I'll "walk&quot
echo $b; // I'll "walk" the <b>
?>
Ref: http://www.php.net/html_entity_decode
I hope this helps solve your issue of unescaped html.

How to replace all <br /> using str_replace?

New to this, I'm trying (and failing) to use str_replace to replace all tags in an html page.
In trying to learn what I'm doing, I can use it with echo to output a modified phrase... but not to replace anything.
I'm using: I'm using <?php // $el = str_replace('<br />', '', $el); ?>
My very limited understanding is that...
< ?php
$str_rep = "This is the original phrase";
echo str_replace("original","modified",$str_rep);
?>
... will modify and display a phrase wherever the code is positioned, but I don't understand how to modify/use to change something already in the code.
It's rather easy:
echo str_replace("<br/>","something else",$text);
Please google before asking on SO.
Your code seems good to me. I used this:
<?php
$el = 'Some text with tags.<br />and no other things<br />Nice!';
$el = str_replace('<br />', '', $el);
echo $el;
and it outputs:
Some text with tags.and no other thingsNice!
Maybe your input is not correct or you are trying to replace the wrong (written) tag?

Really big HTML and Javascript to PHP variable

I have got a Html and Javascript code, that contains about 1000 lines and I need to put it to php variable.
Sure I was thinking about the EOT method, But there is one problem with it, if there is word function like in javascript is, it will take it like php function, and this will cause errors.
Any other Idea how to do it?
I have already tried other forums, but they can't help me, so I hope they can help me on the best.
Maybe use output buffering...
<?php
ob_start();
?>
<b>
<u>
<font color="#FF0000">
<blink>
<marquee>
1000
LINES
OF
HTML
AND
JAVASCRIPT!
</marquee>
</blink>
</font>
</u>
</b>
<?php
$content = ob_get_contents();
ob_clean();
?>
Then your HTML and JavaScript will be in the $content variable.
You could read directly from an HTML file on disk, using file_get_contents().
You can use the EOF method.
There's no problem with reserved words in that case. (As far as I know)
EDIT:
$output .= <<<HTML
function bla()
{
//Something
}
HTML;
Won't be treated as a php function.
Try this;
class Temp
{
public function html($path)
{
ob_start()
require(path); // or file_get_contents(<URI>);
$html = ob_get_clean ();
return $html
}
}
$temp = new Temp();
$htmlData = $temp->html('somepath/somefile.php')
echo $htmlData;

how to escape special characters in php

I am posting this content in the Editor.
> <div class="faq"><ul><li><span class="q">What is a teest?</span>
> <span class="a">A test.</span><div class="spacer"></div></li> </ul></div>
It is saving in the following format.
<p><div class=\\\"faq\\\"><ul><li><span class=\\\"q\\\">What is a teest?</span> <br /><span class=\\\"a\\\">A test.</span><div class=\\\"spacer\\\"></div></li> </ul></div></p>
So the output is not showing perfectly. I have added stripslashes and addslashes. Even it doesn't work.
What is the best way to resolve this issue?
Thanks in advance...
That's ugly..
You need stripslashes() twice, strip_tags() and htmlspecialchars_decode(), if you want working HTML code.
$html = '<p><div class=\\\"faq\\\"><ul><li><span> ...';
$html = htmlspecialchars_decode(strip_tags(stripslashes(stripslashes( $html ))));
echo $html;
This will print:
<div class="faq"><ul><li><span class="q">What is a teest?</span> ...

PHP: Breaking out of PHP for HTML but return as value instead of print on page?

I am generating a lot of HTML code via PHP, but I need to store it in a variable, not display it immediately. But I want to be able to break out of PHP so my code isnt a giant string.
for example (but actual code will be much larger):
<?php
$content = '<div>
<span>text</span>
link
</div>';
?>
I want to do something like this:
<?php
$content =
?>
<div>
<span>text</span>
link
</div>
<?php
;
?>
But I know this will not return the html as a value it will just print it to the document.
But is there a way to do this tho?
Thanks!
You can use output buffering:
<?php
ob_start();
?>
<div>
<span>text</span>
link
</div>
<?php
$content = ob_get_clean();
?>
Or a slightly different method is HEREDOC syntax:
<?php
$content = <<<EOT
<div>
<span>text</span>
link
</div>
EOT;
?>
Try this:
<?php
$var = <<<EOD
my long string
EOD;
echo $var;
?>
(edit done, but one has edit faster than me :))
In my opinion, you should look into a template engine such as Smarty. It can help you take some of the ugliness out of hardcoding HTML into the PHP file, and can help make the code more manageable.
You could store the html in an html file and read that file into a variable.
While still technically a string, you might find the documentation on PHP's heredoc to be an entertaining read:
heredoc syntax
$content = <<<EOT
<div>
<span>text</span>
link
</div>
EOT;
Use heredoc syntax.

Categories