I get iframe from some api and i hold this iframe in some var.
I want to search for "height" and change his value to something else. the same with "scrolling".
For example:
<iframe src="someurl.com" width="540" height="450" scrolling="yes" style="border: none;"></iframe>
After the php function the iframe will be:
we have change the "height" to 600px and "scrolling" to no
<iframe src="someurl.com" width="540" height="600" scrolling="no" style="border: none;"></iframe>
i have solution with this code:
$iframe = preg_replace('/(<*[^>]*height=)"[^>]+"([^>]*>)/', '\1"600"\2', $iframe);
the problem is that after the "preg_replace" run it remove all html attributes after the "height"
Thanks
You can use DOMDocument for it. Something like this:
function changeIframe($html) {
$dom = new DOMDocument;
$dom->loadHTML($html);
$iframes = $dom->getElementsByTagName('iframe');
if (isset($iframes[0])) {
$iframes[0]->setAttribute('height', '600');
$iframes[0]->setAttribute('scrolling', 'no');
return $dom->saveHTML($iframes[0]);
} else {
return false;
}
}
$html = '<iframe src="someurl.com" width="540" height="450" scrolling="yes" style="border: none;"></iframe>';
echo changeIframe($html);
With this method you can modify iframe as you want.
Thanks.
An example as you requested:
$str = '<iframe src="someurl.com" width="540" height="450" scrolling="yes" style="border: none;"></iframe>';
$str = preg_replace('/height=[\"\'][0-9]+[\"\']/i', 'height="600"', $str);
$str = preg_replace('/scrolling=[\"\']yes[\"\']/i', 'scrolling="no"', $str);
echo $str; // -> '<iframe src="someurl.com" width="540" height="600" scrolling="no" style="border: none;"></iframe>'
In fact I want to get an image URL [src] from HTML codes by image alt in PHP, but there is no choice for me.
<?php
$html = '
<img border="0" src="/images/image11.jpg" alt="Image11" width="100" height="100" />
<img border="0" src="/images/image22.jpg" alt="Image22" width="100" height="100" />';
$doc = new DOMDocument();
$doc->loadHTML($html);
$xpath = new DOMXPath($doc);
$src = $xpath->evaluate("string(//img/#src)");
// will return /images/image.jpg
echo $src;
?>
This code is just getting src of the image that has alt="Image22".
$html = '<img border="0" src="/images/image11.jpg" alt="Image11" width="100" height="100" /><img border="0" src="/images/image22.jpg" alt="Image22" width="100" height="100" />';
$dom = new DomDocument();
$dom->loadHTML($html);
$images = $dom->getElementsByTagName('img');
foreach ($images as $key => $image){
if ($image->hasAttributes()){
if ($image->hasAttribute("src")){
if ($image->hasAttribute("alt")){
$alt = $image->getAttribute("alt");
if ($alt == 'Image22'){
$src = $image->getAttribute("src");
echo "<br>" . $src . "<br>";
}
}
}
}
}
<?php
$html = '
<img border="0" src="/images/image11.jpg" alt="Image11" width="100" height="100" />
<img border="0" src="/images/image22.jpg" alt="Image22" width="100" height="100" />';
$doc = new DOMDocument();
$doc->loadHTML($html);
$xpath = new DOMXPath($doc);
$res = $xpath->query('//img');
foreach($res as $img) {
echo $img->getAttribute('alt');
}
?>
Here is a small snippet of the PHP-Code I use to create a HTML e-mail:
$tmpl = '<table border="0" width="100%";><tr><td>%title%</td></tr><tr><td>%text%</td></tr></table>';
$html = '<table border="0" width="100%";>';
$html .= '<td width="20%" style="padding:0;">';
if(isset($var)) {
$html .= 'Value: '.$object->val;
}
$html .= '</td>';
$html .= '</table>';
$tmpl = str_replace('%text%', $html, $tmpl);
$mail->setBody( $tmpl );
$mail->send();
Sometimes the mail in the HTML view when viewed inside an email program is broken because there is a space character inside an opening TD-Element. Like this:
< td width="20%" style="padding:0;">
Where is this space character coming from?
Had the same issue with php mail() function, my html styling was broken by seemingly random spaces. Using wordwrap() on the message before sending it with mail sorted out the problem.
$message = wordwrap($message, 70);
mail("person#example.com","Some subject",$message);
www.w3schools.com explanation, example
See this line here?
$html = '<table border="0" width="100%";>';
after width="100%" you do not need this symbol -> ;
Try removing it.
Markup has errors
$tmpl = '<table border="0" width="100%";><tr><td>%title%</td></tr><tr><td>%text%</td></tr></table>';
Notice the ; in border="0" width="100%";>
You're not concatenating $html
The following line overwrite the table built up earlier
$html = '</table>';
You need to change that to;
$html .= '</table>';
Suggested actions
I would suggest echoing the body of the mail, and validating it with w3.
Have a read...
Concatenating
Validate your markup
<?php
$tmpl = '
<table border="0" width="100%">
<tr>
<td>%title%</td>
</tr><tr>
<td>%text%</td>
</tr>
</table>';
$html = '<table border="0" width="100%">';
$html .= '<td width="20%" style="padding:0;">';
if(isset($var)) {
$html .= 'Value: '.$object->val;
}
$html .= '</td>';
$html .= '</table>';
//Debug:
// echo $html;
$tmpl = str_replace('%text%', $html, $tmpl);
$mail->setBody( $tmpl );
$mail->send();
Of these four links:
<img border="0" src="imagenes/flech.gif" width="6" height="8">
Albano Y Romina Power<br>
<img border="0" src="imagenes/flech.gif" width="6" height="8">
Armando Manzanero<br>
<a name="inicio21" href="musica-Merengue-de-Banda_Cuisillos.html">
<img border="0" src="imagenes/flech.gif" width="6" height="8">Banda Cuisillos</a><br>
Baladas Alternativas<br>
I'm trying to capture the href value and the text of the link of the three first, leaving out the fourth link, in other words i'm trying to get this:
escuchar-baladas-de-Albano_Y_Romina_Power.html Albano Y Romina Power
escuchar-baladas-de-Armando_Manzanero.html Armando Manzanero
musica-Merengue-de-Banda_Cuisillos.html Banda Cuisillos
I was trying to make the most of the fact that the three first have imagenes/flech.gif and that way leave out the fourth, the thing that imagenes/flech.gif isn't in the same order. Here is my attempt to solve it where i get up to the href but include the fourth.
Thanks for any help
You should use an html parser and not a regex, try this:
<?php
$html = <<< EOF
<img border="0" src="imagenes/flech.gif" width="6" height="8">
Albano Y Romina Power<br>
<img border="0" src="imagenes/flech.gif" width="6" height="8">
Armando Manzanero<br>
<a name="inicio21" href="musica-Merengue-de-Banda_Cuisillos.html">
<img border="0" src="imagenes/flech.gif" width="6" height="8">Banda Cuisillos</a><br>
Baladas Alternativas<br>
EOF;
$dom = new DOMDocument();
#$dom->loadHTML($html);
# Iterate over all the <a> tags
foreach($dom->getElementsByTagName('a') as $link) {
$url = $link->getAttribute('href');
$text = preg_replace('/[\r\n]/sm', '', $link->nodeValue); // remove line breaks
//if doesn't contain the banned words...
if (!preg_match('/(Baladas Alternativas|another text to filter)/sm', $text)) {
echo $url ." ".$text. "\n";
}
}
?>
DEMO
http://ideone.com/5QX83x
RESOURCES
http://htmlparsing.com/php.html
this code will get the first 3 links
$a='<img border="0" src="imagenes/flech.gif" width="6" height="8">Albano Y Romina Power<br><img border="0" src="imagenes/flech.gif" width="6" height="8">Armando Manzanero<br><a name="inicio21" href="musica-Merengue-de-Banda_Cuisillos.html"><img border="0" src="imagenes/flech.gif" width="6" height="8">Banda Cuisillos</a><br>Baladas Alternativas<br>';
preg_match_all('/<a.*?href="(.+?)">(?:<img.*\d+">)?(.+?)<\/a>/',$a,$match);
echo $match[1][0] . " " . $match[2][0]."<br>";
echo $match[1][1] . " " . $match[2][1]."<br>";
echo $match[1][2] . " " . $match[2][2]."<br>";
I had data in my table like this:
#inlcude<stdio.h>
main() {
printf("Hi");
}
But when I retrieve data from the database, it displays:
#inlude<stdio.h>main() { printf("Hi"); }
As you can see, there are no line breaks. Here is my entire code:
function BBCode ($str) {
$str = htmlentities($str);
$simple_search = array(
//added line break
'/\[br\]/is',
'/\[b\](.*?)\[\/b\]/is',
'/\[i\](.*?)\[\/i\]/is',
'/\[u\](.*?)\[\/u\]/is',
'/\[url\=(.*?)\](.*?)\[\/url\]/is',
'/\[url\](.*?)\[\/url\]/is',
'/\[align\=(left|center|right)\](.*?)\[\/align\]/is',
'/\[img\](.*?)\[\/img\]/is',
'/\[mail\=(.*?)\](.*?)\[\/mail\]/is',
'/\[mail\](.*?)\[\/mail\]/is',
'/\[font\=(.*?)\](.*?)\[\/font\]/is',
'/\[size\=(.*?)\](.*?)\[\/size\]/is',
'/\[color\=(.*?)\](.*?)\[\/color\]/is',
//added textarea for code presentation
'/\[codearea\](.*?)\[\/codearea\]/is',
//added paragraph
'/\[p\](.*?)\[\/p\]/is',
//smilies
'/\:angry:/is',
'/\:angel:/is',
'/\:arrow:/is',
'/\:at:/is',
'/\:biggrin:/is',
'/\:blank:/is',
'/\:blush:/is',
'/\:confused:/is',
'/\:cool:/is',
'/\:dodgy:/is',
'/\:exclamation:/is',
'/\:heart:/is',
'/\:huh:/is',
'/\:lightbulb:/is',
'/\:my:/is',
'/\:rolleyes:/is',
'/\:sad:/is',
'/\:shy:/is',
'/\:sleepy:/is',
'/\:smile:/is',
'/\:tongue:/is',
'/\:undecided:/is',
'/\:wink:/is',
// For 3rd Party Code
'/\[code\](.*?)\[\/code\]/is',
'/\[php\](.*?)\[\/php\]/is',
'/\[css\](.*?)\[\/css\]/is',
'/\[jscript\](.*?)\[\/jscript\]/is',
'/\[sql\](.*?)\[\/sql\]/is',
'/\[perl\](.*?)\[\/perl\]/is',
);
$simple_replace = array(
//added line break
'<br />',
'<strong>$1</strong>',
'<em>$1</em>',
'<u>$1</u>',
// added nofollow to prevent spam
'$2',
'$1',
'<div style="text-align: $1;">$2</div>',
//added alt attribute for validation
'<img src="$1" alt="" />',
'$2',
'$1',
'<span style="font-family: $1;">$2</span>',
'<span style="font-size: $1;">$2</span>',
'<span style="color: $1;">$2</span>',
//added textarea for code presentation
'<textarea class="code_container" rows="20" cols="65">$1</textarea>',
//added paragraph
'<p>$1</p>',
//smilies
'<img src="comments/smilies/angry.gif" border="0" alt="" />',
'<img src="comments/smilies/angel.gif" border="0" alt="" />',
'<img src="comments/smilies/arrow.gif" border="0" alt="" />',
'<img src="comments/smilies/at.gif" border="0" alt="" />',
'<img src="comments/smilies/biggrin.gif" border="0" alt="" />',
'<img src="comments/smilies/blank.gif" border="0" alt="" />',
'<img src="comments/smilies/blush.gif" border="0" alt="" />',
'<img src="comments/smilies/confused.gif" border="0" alt="" />',
'<img src="comments/smilies/cool.gif" border="0" alt="" />',
'<img src="comments/smilies/dodgy.gif" border="0" alt="" />',
'<img src="comments/smilies/exclamation.gif" border="0" alt="" />',
'<img src="comments/smilies/heart.gif" border="0" alt="" />',
'<img src="comments/smilies/huh.gif" border="0" alt="" />',
'<img src="comments/smilies/lightbulb.gif" border="0" alt="" />',
'<img src="comments/smilies/my.gif" border="0" alt="" />',
'<img src="comments/smilies/rolleyes.gif" border="0" alt="" />',
'<img src="comments/smilies/sad.gif" border="0" alt="" />',
'<img src="comments/smilies/shy.gif" border="0" alt="" />',
'<img src="comments/smilies/sleepy.gif" border="0" alt="" />',
'<img src="comments/smilies/smile.gif" border="0" alt="" />',
'<img src="comments/smilies/tongue.gif" border="0" alt="" />',
'<img src="comments/smilies/undecided.gif" border="0" alt="" />',
'<img src="comments/smilies/wink.gif" border="0" alt="" />',
// For 3rd Party Code
'<pre class="brush: plain;">$1</pre>',
'<pre class="brush: php;">$1</pre>',
'<pre class="brush: css;">$1</pre>',
'<pre class="brush: jscript;">$1</pre>',
'<pre class="brush: sql;">$1</pre>',
'<pre class="brush: perl;">$1</pre>',
);
// Do simple BBCode's `
$str = preg_replace ($simple_search, $simple_replace, $str);
return $str;
}
function getComments($tutid){
//fetch all comments from database where the tutorial number is the one you are asking for:
$results = mysql_query("SELECT * FROM comments WHERE qazi_id='$tutid' ORDER BY id DESC") or die(mysql_error());
//find the number of comments:
$commentNum = mysql_num_rows($results);
echo' <div class="post-bottom-section">
<h2> '.$commentNum.' comments so far (post your own)</h2>
<div class="primary">
<ol class="commentlist">';
$count = mysql_numrows($results);
$i = 0;
while ($i < $count){
$qazi_id = mysql_result($results,$i,"qazi_id");
$name = mysql_result($results,$i,"name");
$email = mysql_result($results,$i,"email");
$description =mysql_result($results,$i,"description");
$url = mysql_result($results,$i,"url");
$date = mysql_result($results,$i,"date");
echo' <li class="'.$css.'">
<div class="comment-info">
<img alt="" src="'.$grav_url.' class="avatar" height="42" width="42" />
<cite>
'.$name.' Says: <br />
<span class="comment-data">'.$date.'</span>
</cite>
</div>
<div class="comment-text">
<p>'.BBCode($description).'</p>
</div>
</li>';
$i++;
}
echo'</ol></div></div>';
}`
If I replace $description =mysql_result($results,$i,"description"); with $description =nl2br(mysql_result($results,$i,"description")); the code displays output as
#include<stdio.h><br /> main() <br /> { <br /%gt; printf("hi");<br /> }
<br /> tags appears.
Jest replace return $str;
with
return nl2br($str);