How do I erase all styles from HTML using PHP? [duplicate] - php

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
php regexp: remove all attributes from an html tag
$input = '<div style="font-size: 18px; line-height: 20px;" id="whatever">text</div>';
// ... some code here, probably regex
$desired_output = '<div id="whatever">text</div>';
How do I do the above using PHP?

try the following you need a regular expression to strip it out.
$desired_output = preg_replace('/<\s*style.+?<\s*\/\s*style.*?>/si', ' ', $input );
or this
$desired_output = preg_replace('%style="[^"]+"%i', '', $input);

Related

Transforming <span style="font-weight:bold">some text<span> into <b>some text</b> in PHP [duplicate]

This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 3 years ago.
As title, If I have some html <p><span style="font-style:italic">abcde</span><span style="font-weight:bold">abcde</span></p>, I want to strip the style tags and transform them into html tags, so to make it become <p><i>abcde</i><b>abcde</b></p>. How can I do that in PHP?
I notice that when I open the html in CKEditor, this kind of transformation is done automatically. But I want to do it in backend PHP. Thanks.
$string = '<p><span style="font-style-italic;font-weight:bold">abcde</span><span style="font-weight:bold">abcde</span></p>';
$dom = new DOMDocument();
$dom->loadHTML($string);
$xp = new DOMXPath($dom);
$str = '';
$results = $xp->query('//span');
if($results->length>0){
foreach($results as $result){
$style = $result->getAttribute("style");
$style_arr = explode(";",$style);
$style_template = '%s';
if(count($style_arr)>0){
foreach($style_arr as $style_item){
if($style_item == 'font-style-italic'){
$style_template = '<i>'.$style_template.'</i>';
}
if($style_item == 'font-weight:bold'){
$style_template = '<b>'.$style_template.'</b>';
}
}
}
$str .= sprintf($style_template,$result->nodeValue);
}
}
$str = '<p>'.$str.'</p>';
You can also use html tags under php parameters or php opening and closing tags like this
<?php
echo"<h1>Here is Heading h1 </h1>";
?>
Or you can Put your html code in " " after echo
Like this
<?php
echo"Your Html Code Here";
?>
$output = preg_replace('/(<[^>]+) style=".*?"/i', '$1', $input);
Match a < follow by one or more and not > until space came and the style="anything" reached. The /i will work with capital STYLE and $1 will leave the tag as it is, if the tag does not include style="". And for the single quote style='' use this:
(<[^>]+) style=("|').*?("|')

Rewriting HTML anchor using str_replace and regex [duplicate]

This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 3 years ago.
The following is automatically output via editor:
1001-web-file
I would like to add an ID:
<a id="replace" href="https://someurl.com/1001-web-file.pdf">1001-web-file</a>
Then use str_replace and a regular expression to find the anchors in question and replace with:
<img src="https://someurl.com/1001-web-file-pdf-290x300.jpg"/>
What I've managed to do is:
$replace = array(
'<a id="pdfthumb" href="' => '<img src="',
'.pdf">pdfthumb</a>' => '-pdf-290x300.jpg"/></br>'
);
$text = str_replace(array_keys($replace), $replace, $text);
return $text;
This works to tear down the anchor tag and rebuild as an img. But I can't do much more. I played around with some regex to create a wildcard and realized I need to create a variable for the href to use when I rebuild the HTML, but I'm stuck.
Any insight is much appreciated :)
In your case, I think it should be easier if you do it on client side using javascript.
My background is not PHP but you can use the same pattern to test with your PHP code:
Input:
1001-web-file
Output:
<img src="https://someurl.com/1001-web-file-pdf-290x300.jpg"/>
var input = '1001-web-file';
var pattern = /href=\"(.+)\.pdf\"/;
var match = input.match(pattern)[1];
input = input.replace(/(<a.*>).*(<\/a>)/, '$1<img src="' + match + '-pdf-290x300.jpg">$2');
console.log(input)
Here is translated php code of Tan javascript answer
$input = '1001-web-file';
preg_match('/href="(.+)\.pdf"/', $input, $m);
$match = $m[1];
$input = preg_replace('/(<a.*>).*(<\/a>)/', '$1<img src="'. $match .'-pdf-290x300.jpg">$2', $input);
echo $input;

php preg_match add href .html [duplicate]

This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 5 years ago.
I would like to change all href's in a website.
xyz …
to:
<a href="sitename.html>xyz …</a>
Thanks for help!
You could do it using the following way ...
$hrefs = 'xyz …
xyz …';
$r = '/(?<=href=").*?(?=">)/';
$sitename = 'sitename.html';
$result = preg_replace($r, $sitename, $hrefs);
echo $result;
DEMO
$content = file_get_contents('page.html');
$new_content = str_replace('<a href="', '<a href="sitename.html', $content);
echo $new_content;

Regular Expression not match href strings [duplicate]

This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 7 years ago.
I have the following regex:
$regex = '<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>(.*)<\/a>';
how can I improve this to NOT match the word "files" or "resize" in the href tag:
link or
yes parsing is the mutch better way to do this - maybe someone find this helpful:
$inhalt = new DOMDocument;
$inhalt->loadHTML($content->draw()[0][0]);
foreach ($inhalt->getElementsByTagName('a') as $node) {
if ($node->hasAttribute('href')) {
if (preg_match("/(files|resize)/", $node->getAttribute('href')) == 0) {
$node->setAttribute('href', 'mobile.php?uri=http://www.example.com' . str_replace("..", "", $node->getAttribute('href')));
$inhalt->saveHtml($node);
}
}
}
echo $inhalt->saveHtml();
You can use this regex to get all href string:
<a[^>]*href=[\"\'](.*?)[\"\'][^>]*>(.*?)</a>

Add a word in php code [duplicate]

This question already has answers here:
How can I combine two strings together in PHP?
(19 answers)
Closed 7 years ago.
in this code, I need to add a simple text "from" after the calendar icon.
How can i do?
Thank you
$html .= '<span class="dashicons dashicons-calendar"></span>' . $datetime->date_range( $date_format ) . '<br/>';
You can do it like this:
$html .= '<span class="dashicons dashicons-calendar"></span>'.'From'. $datetime->date_range( $date_format ) . '<br/>';

Categories