Delete tag html on PHP - php

How to delete tag html on php
my data:
$a="<div style='color: blue;'>Hallo</div>
<div>yas!</div><img src='blabla/aa/img.png'> im fine
<span> yes</span> <a href='aaa.com'>link</a>";
How to delete all div and span
<div style='color: blue;'></div>, <div></div> and <span></span>
and not delete
<img src='blabla/aa/img.png'> and <a href='aaa.com'>link</a>
Please help me

How about using strip_tags:
$a="<div style='color: blue;'>Hallo</div>"
."<div>yas!</div><img src='blabla/aa/img.png'> im fine<span> yes</span>";
$clean_html = strip_tags($a, "<img><a>");

If you have a white list of element then you can use the strip_tags() function http://php.net/manual/en/function.strip-tags.php.
Another option is to use http://htmlpurifier.org
WhiteList
http://htmlpurifier.org/live/configdoc/plain.html#HTML.AllowedElements
Blacklist
http://htmlpurifier.org/live/configdoc/plain.html#HTML.ForbiddenElements

Your sentence cant explain what is your goal. Have a look at this.
substr — Return part of a string
Description
string substr ( string $string , int $start [, int $length ] )
Returns the portion of string specified by the start and length parameters.
Parameters
string
The input string. Must be one character or longer.
start
If start is non-negative, the returned string will start at the start'th position in string, counting from zero. For instance, in the string 'abcdef', the character at position 0 is 'a', the character at position 2 is 'c', and so forth.
If start is negative, the returned string will start at the start'th character from the end of string.
If string is less than or equal to start characters long, FALSE will be returned.
Examples
Example - Basic substr() usage
<?php
echo substr('abcdef', 1); // bcdef
echo substr('abcdef', 1, 3); // bcd
echo substr('abcdef', 0, 4); // abcd
echo substr('abcdef', 0, 8); // abcdef
echo substr('abcdef', -1, 1); // f
// Accessing single characters in a string
// can also be achieved using "square brackets"
$string = 'abcdef';
echo $string[0]; // a
echo $string[3]; // d
echo $string[strlen($string)-1]; // f
?>
Or
If you want to remove HTML tags and only allow some of them you can use strip_tags function like this
$a="<div style='color: blue;'>Hallo</div>"."<div>yas!</div><img src='blabla/aa/img.png'> im fine<span> yes</span>";
$clean_html = strip_tags($a, "");
This will return the text you entered $text with no tags except & tags You can read more about strip_tags
Source : http://us2.php.net/manual/en/function.substr.php

You Can use this:
<?php
$a="<div style='color: blue;'>Hallo</div><div>yas!</div><img src='blabla/aa/img.png'> im fine<span> yes</span>";
echo strip_tags($a,"<img><a>");
?>

http://php.net/manual/en/function.preg-replace.php
$a="<div style='color: blue;'>Hallo</div>
<div>yas!</div><img src='blabla/aa/img.png'> im fine<span> yes</span>";
$find=[
"/<div style='color: blue;'>.*.<\/div>/",
"/<div>.*.<\/div>/",
"/<span>.*.<\/span>/"
];
echo preg_replace($find,"",$a);

Related

how to do echo from a string, only from values that are between a specific stretch[href tag] of the string?

[PHP]I have a variable for storing strings (a BIIGGG page source code as string), I want to echo only interesting strings (that I need to extract to use in a project, dozens of them), and they are inside the quotation marks of the tag
but I just want to capture the values that start with the letter: N (news)
[<a href="/news7044449/exclusive_news_sunday_"]
<a href="/n[ews7044449/exclusive_news_sunday_]"
that is, I think you will have to work with match using: [a href="/n]
how to do that to define that the echo will delete all the texts of the variable, showing only:
note that there are other hrefs tags with values that start with other letters, such as the letter 'P' : href="/profiles... (This does not interest me.)
$string = '</div><span class="news-hd-mark">HD</span></div><p>exclusive_news_sunday_</p><p class="metadata"><span class="bg">Czech AV<span class="mobile-hide"> - 5.4M Views</span>
- <span class="duration">7 min</span></span></p></div><script>xv.thumbs.preparenews(7044449);</script>
<div id="news_31720715" class="thumb-block "><div class="thumb-inside"><div class="thumb"><a href="/news31720715/my_sister_running_every_single_morning"><img src="https://static-hw.xnewss.com/img/lightbox/lightbox-blank.gif"';
I imagine something like this:
$removes_everything_except_values_from_the_href_tag_starting_with_the_letter_n = ('/something regex expresion I think /' or preg_match, substring?);
echo $string = str_replace($removes_everything_except_values_from_the_href_tag_starting_with_the_letter_n,'',$string);
expected output: /news7044449/exclusive_news_sunday_
NOTE: it is not essential to be through a variable, it can be from a .txt file the place where the extracts will be extracted, and not necessarily a variable.
thanks.
I believe this will help her.
<?php
$source = file_get_contents("code.html");
preg_match_all("/<a href=\"(\/n(?:.+?))\"[^>]*>/", $source, $results);
var_export( end($results) );
Step by Step Regex:
Regex Demo
Regex Debugger
To get just the links out of the $results array from Valdeir's answer:
foreach ($results as $r) {
echo $r;
// alt: to display them with an HTML break tag after each one
echo $r."<br>\n";
}

PhP Find (and replace) string between two different strings

I have a string, that look like this "<html>". Now what I want to do, is get all text between the "<" and the ">", and this should apply to any text, so that if i did "<hello>", or "<p>" that would also work. Then I want to replace this string with a string that contains the string between the tags.
For example
In:
<[STRING]>
Out:
<this is [STRING]>
Where [STRING] is the string between the tags.
Use a capture group to match everything after < that isn't >, and substitute that into the replacement string.
preg_replace('/<([^>]*)>/, '<this is $1>/, $string);
here is a solution to test on the pattern exists and then capture it to finally modify it ...
<?php
$str = '<[STRING]>';
$pattern = '#<(\[.*\])>#';
if(preg_match($pattern, $str, $matches)):
var_dump($matches);
$str = preg_replace($pattern, '<this is '.$matches[1].'>', $str);
endif;
echo $str;
?>
echo $str;
You can test here: http://ideone.com/uVqV0u
I don't know if this can be usefull to you.
You can use a regular expression that is the best way. But you can also consider a little function that remove first < and last > char from your string.
This is my solution:
<?php
/*Vars to test*/
$var1="<HTML>";
$var2="<P>";
$var3="<ALL YOU WANT>";
/*function*/
function replace($string_tag) {
$newString="";
for ($i=1; $i<(strlen($string_tag)-1); $i++){
$newString.=$string_tag[$i];
}
return $newString;
}
/*Output*/
echo (replace($var1));
echo "\r\n";
echo (replace($var2));
echo "\r\n";
echo (replace($var3));
?>
Output give me:
HTML
P
ALL YOU WANT
Tested on https://ideone.com/2RnbnY

Remove special characters like lt; but not anchor tag

How can I remove special characters like ;lt ;gt but not Anchor tag
e.g
&lt;a href=&quot;http://www.imdb.com/name/nm0005069/&quot;&gt;Spike Jonze&lt;/a&gt; This cause by <a class="primary-black" href="http://example.com/community/RobHallums">RobHallums</a>
should be
Spike Jonze This cause by <a class="primary-black" href="http://example.com/community/RobHallums">RobHallums</a>
Here's a quick one for you:
<?php
// SET OUR DEFAULT STRING
$string = '&lt;a href=&quot;http://w...content-available-to-author-only...b.com/name/nm0005069/&quot;&gt;Spike Jonze&lt;/a&gt; This cause by <a class="primary-black" href="http://e...content-available-to-author-only...e.com/community/RobHallums">RobHallums</a>';
// USE PREG_REPLACE TO STRIP OUT THE STUFF WE DON'T WANT
$string = preg_replace('~&lt;.*?&gt;~', '', $string);
// PRINT OUT OUR NEW STRING
print $string;
All I'm doing here is looking for &lt;, followed by any character ., any number of times *, until it matches the next part of the string ?, which is &gt;.
Any time it finds that, it replaces it with nothing. So you're left with the text you want.
Here is a working demo:
http://ideone.com/uSnY0b
use html_entity_decode:
<?php $url = html_entity_decode('&lt;a href=&quot;http://www.imdb.com/name/nm0005069/&quot;&gt;Spike Jonze&lt;/a&gt;');
echo $url;
?>
the output will be:
Spike Jonze
EDIT:
<?php
preg_match_all('/<a .*?>(.*?)<\/a>/',$url,$matches);
//For Text Name
echo $matches[1][0]; //output : Spike Jonze
?>

php preg_match_all and preg_replace.

[caption id="attachment_1342" align="alignleft" width="300" caption="Cheers... "Forward" diversifying innovation to secure first place. "][/caption] A group of 35 students from...
I'm reading this data from api. I want the text just start with A group of 35 students from.... Help me to replace the caption tag with null. This is what I tried:
echo "<table>";
echo "<td>".$obj[0]['title']."</td>";
echo "<td>".$obj[0]['content']."</td>";
echo "</table>";
$html = $obj[0]['content'];
preg_match_all('/<caption>(.*?)<\/caption>/s', $html, $matches);
preg_replace('',$matches, $obj[0]['content']);
Any help.
$pattern = "/\[caption (.*?)\](.*?)\[\/caption\]/i";
$removed = preg_replace($pattern, "", $html);
echo preg_replace("#\[caption.*\[/caption\]#u", "", $str);
In the snippet mentioned in the question, regex search pattern is incorrect. there is no <caption> in the input. its <caption id....
Second using preg_replace doesn't serve any purpose here. preg_replace expects three arguments. first should be a regex pattern for search. second the string to replace with. and third is input string.
Following snippet using preg_match will work.
<?php
//The input string from API
$inputString = '<caption id="attachment_1342" align="alignleft" width="300" caption="Cheers... "Forward" diversifying innovation to secure first place. "></caption> A group of 35 students from';
//Search Regex
$pattern = '/<caption(.*?)<\/caption>(.*?)$/';
//preg_match searches inputString for a match to the regular expression given in pattern
//The matches are placed in the third argument.
preg_match($pattern, $inputString, $matches);
//First match is the whole string. second if the part before caption. third is part after caption.
echo $matches[2];
// var_dump($matches);
?>
if you still want to use preg_match_all for some reason. following snippet is modification of the one mentioned in question -
<?php
//Sample Object for test
$obj = array(
array(
'title' => 'test',
'content' => '<caption id="attachment_1342" align="alignleft" width="300" caption="Cheers... "Forward" diversifying innovation to secure first place. "></caption> A group of 35 students from'
)
);
echo "<table border='1'>";
echo "<td>".$obj[0]['title']."</td>";
echo "<td>".$obj[0]['content']."</td>";
echo "</table>";
$html = $obj[0]['content'];
//preg_match_all will put the caption tag in first match
preg_match_all('/<caption(.*?)<\/caption>/s', $html, $matches2);
//var_dump($matches2);
//use replace to remove the chunk from content
$obj[0]['content'] = str_replace($matches2[0], '', $obj[0]['content']);
//var_dump($obj);
?>
Thank you guys. I use explode function to do this.
$html = $obj[0]['content'];
$code = (explode("[/caption]", $html));
if($code[1]==''){
echo $code[1];
}

Remove ellipsis (&helip;) after sub-string concatenation

I am concatenating two sub-strings with an ellipsis (...) added at the end of the first sub-string. However I want this elipsis to be removed after the concatenation.
Is this possible via a script or other means, Thanks:
<?php echo substr($post_text_result2, 0, 400) . "…";?><div id="second_post" class = "hidden"><?php echo substr($post_text_result2, 400, 5000);?></div>:
str_replace deletes the elipsis like requested, however does not fully work in my situation:
The code below works however, The first sub-string is repeated. I need a way to remove the first sub-string.
<?php
$string = substr($post_text_result2, 0, 400) . "…";
echo $string;
?>
<div id="second_post" class = "hidden">
<?php
$string= str_replace('…','',$string); echo $string;
echo $string;
echo substr($post_text_result2, 400, 5000);
?>
easy
<?php
$string = substr($post_text_result2, 0, 400) . "…";
$string = str_replace('…','',$string);
?>
If you want an ellipsis always at the end, just do this:
<?php
$string = substr($post_text_result2, 0, 400) . "…";
$string = str_replace('…','',$string) . "…";
?>
You can store the length of the first string A and then build a new string concatenating 2 substrings:
substring 1 from 0 until length(A) - 3
substring 2 from length(A) until total length
You also could use a regex and replace "..." with "", but that could remove other "..." which is maybe unwanted.
Sorry for no code, I can't PHP but you know how to do it ;)

Categories