My string:
some text some text < b >some text< /b > some text < a href = http ://sometextsometext< b >sometext< /b >sometextsometext >text< /a > some text some text
Is there any way using preg_match or str_replace to only remove the < b >< /b > in the link tag?
Thanks
Okay, so here's an idea using PHP's function preg_replace_callback
<?php
// SET TEXT TO BE USED
$string = 'some text some text <b>some text</b> some text <a href=http://sometextsometext<b>sometext</b>sometextsometext>text</a> some text some text. And We Have A <a href=http://google.com>Google</a> Link';
// USE A CALLBACK FUNTION TO SCAN THROUGH LINKS
$string = preg_replace_callback('~<a.*?</a>~', 'remove_crap_from_links', $string);
print $string;
// THIS IS THE CALLBACK FUNCTION ... EACH LINK IS STORED AS '$m'
function remove_crap_from_links($m) {
// PULL OUT THE PART OF THE LINK BEFORE THE CLOSING LINK BRACKET
// (USE A NEGATIVE LOOKAHEAD TO MAKE SURE THAT IT CAN'T HAVE ANY OPENING/CLOSING HTML BRACKETS IN THERE
if (preg_match('~<a(.*?)>(?:[^<>]*?)</a>~i', $m[0], $url_matches)) {
// RUN A PHP strip_tags FUNCTION TO PULL OUT ANY HTML TAGS FOUND IN THE LINK BODY
$stripped_url = strip_tags($url_matches[1]);
// REBUILD THE URL, USING THE $stripped_url IN PLACE OF WHAT WAS ALREADY THERE
$clean_url = preg_replace('~(<a)(.*?)(>(?:[^<>]*?)</a>)~', '$1'.$stripped_url.'$3', $m[0]);
}
return $clean_url;
}
So basically, I'm taking the part that has been suggested a couple of times with PHP's strip_tags function, but only using on parts that it finds inside of link tags.
Here is a working demo
You can use strip_tags function in php to remove html tags.
$text = 'Test paragraph. Other text';
echo strip_tags($text);
Output : Test paragraph. Other text
strip_tags - Strip HTML and PHP tags from a string
PHP Code
<?php
$text = '<p>Test paragraph.</p><!-- Comment --> Other text';
echo strip_tags($text);
echo "\n";
// Allow <p> and <a>
echo strip_tags($text, '<p><a>');
?>
The above example will output:
Test paragraph. Other text
<p>Test paragraph.</p> Other text
Ref: http://php.net/manual/en/function.strip-tags.php
Related
I have a set of <p></p> tags wrapping a set of data, that data includes other tags such as <script></script> however that content could contain any number of different tags.
I just need to remove any paragraph tags from the content
Example below
$text = "<p><script>example text inside script.<script></p>";
I see the strip_tags function but I believe that will remove all the tags.
How would I go about just removing the paragraph?
Thanks.
Try,
$text = "<p><script>example text inside script.<script></p>";
$formatted_text = str_replace(['<p>', '</p>'], '', $text);
You can allow tag with strip_tags()
like this example:
$text = "<p><script>example text inside script.<script></p>";
echo strip_tags($text, '<script>');
Try this.
<?php
$text = "<p><script>example text inside script.<script></p>";
$replace = array('<p>','</p>');
echo str_replace($replace,'',$text);
http://sandbox.onlinephpfunctions.com/code/43150c7af4e7e5f572827d91abca5756213ab7ba
Version 2 (works for classes)
echo preg_replace('%<p(.*?)>|</p>%s','',$text);
http://sandbox.onlinephpfunctions.com/code/6c5414773efc1317578b5f0581b68e5acabb9a2b
Hope this helps.
You can use str_replace(),
add this line -
$text = str_replace('<p>','',$text);
$text = str_replace('</p>','',$text);
It will remove both
<p> and </p>
So i have a string and I used the strip_tags() function to remove all tags except IMG but I still have plain text next to my IMG element. Here a visual example
$myvariable = "This text needs to be removed<a href='blah_blah_blah'>Blah</a><img src='blah.jpg'>"
So using PHP strip_tags() I was able to remove all tags except the <img> tag (which is what I want). But the thing is now it didn't remove the text.
How do I remove the left over text? Text will always either before tag or after tag as well
[ADDED MORE DETAILS]
$description = 'crazy stuff<img src="https://scontent.cdninstagram.com/t51.2885-15/e15/14287934_1389514537744146_673363238_n.jpg?ig_cache_key=MTMzNzM3MzgwNjAyNDY5NDAzMA%3D%3D.2">';
that's what the variable is actually holding.
Thanks in Advance
Instead of replacing something you can very well extract the values you want:
(<(\w+).+</\2>)
To be used with preg_match(), see a demo on regex101.com.
IN PHP:
<?php
$regex = '~(<(\w+).+</\2>)~';
$string = 'crazy stuff<img src="https://scontent.cdninstagram.com/t51.2885-15/e15/14287934_1389514537744146_673363238_n.jpg?ig_cache_key=MTMzNzM3MzgwNjAyNDY5NDAzMA%3D%3D.2">here as well';
if (preg_match($regex, $string, $match)) {
echo $match[1];
}
?>
Please show your whole piece of code with the use of strip_tags.
You can try: preg_replace('~.*(<img[^>]+>)~', '$1', $myvariable);
I want to remove all tags before showing them on preview mode (just some text).
I have this code:
$text = strip_tags($item['content']);
echo substr($text,0,13);
here is my $item['content'] is something like this
<div class="note note-success">
<p>
Font Awesome gives you scalable
vector icons that can instantly be customized — size, color, drop
shadow, and anything that can be done with the power of CSS. The
complete set of 439 icons in Font Awesome 4.1.0
</p>
For more info check out: <a target="_blank" href="http://fortawesome.github.io/Font-Awesome/icons/">http://fortawesome.github.io/Font-Awesome/icons/</a>
</div>
The problem is that when I use substr it doesn't show anything, but when I use normal echo, it shows the content of the variable that was stripped before.
Does strip_tags not give string output?
Try to remove whitespaces before outputting your substring:
$new = str_replace(' ','',$text); (Use trim instead as #mario.klump said)
$text = strip_tags($item['content']);
$new = trim($text);
echo substr($new,0,13);
strip_tags() function works only when following type of html text. what you are doing is convert html encoded text so, it will not be parse.
$text = '<p>Test paragraph.</p><!-- Comment --> Other text';
echo strip_tags($text);
For your example you can use like this:
$text = htmlentities($item['content']);
echo substr(html_entity_decode($text),0,13); or
echo substr($text,0,13);
I am having trouble working out how to do this, I have a string looks something like this...
$text = "<p>This is some example text This is some example text This is some example text</p>
<p><em>This is some example text This is some example text This is some example text</em></p>
<p>This is some example text This is some example text This is some example text</p>";
I basically want to use something like preg_repalce and regex to remove
<em>This is some example text This is some example text This is some example text</em>
So I need to write some PHP code that will search for the opening <em> and closing </em> and delete all text in-between
hope someone can help,
Thanks.
$text = preg_replace('/([\s\S]*)(<em>)([\s\S]*)(</em>)([\s\S]*)/', '$1$5', $text);
In case if you are interested in a non-regex solution following would aswell:
<?php
$text = "<p>This is some example text This is some example text This is some example text</p>
<p><em>This is some example text This is some example text This is some example text</em></p>
<p>This is some example text This is some example text This is some example text</p>";
$emStartPos = strpos($text,"<em>");
$emEndPos = strpos($text,"</em>");
if ($emStartPos && $emEndPos) {
$emEndPos += 5; //remove <em> tag aswell
$len = $emEndPos - $emStartPos;
$text = substr_replace($text, '', $emStartPos, $len);
}
?>
This will remove all the content in between tags.
$text = '<p>This is some example text This is some example text This is some example text</p>
<p><em>This is the em text</em></p>
<p>This is some example text This is some example text This is some example text</p>';
preg_match("#<em>(.+?)</em>#", $text, $output);
echo $output[0]; // This will output it with em style
echo '<br /><br />';
echo $output[1]; // This will output only the text between the em
[ View output ]
For this example to work, I changed the <em></em> contents a little, otherwise all your text is the same and you cannot really understand if the script works.
However, if you want to get rid of the <em> and not to get the contents:
$text = '<p>This is some example text This is some example text This is some example text</p>
<p><em>This is the em text</em></p>
<p>This is some example text This is some example text This is some example text</p>';
echo preg_replace("/<em>(.+)<\/em>/", "", $text);
[ View output ]
Use strrpos to find the first element and
then the last element.
Use substr to get the part of string.
And then replace the substring with empty string from original string.
format: $text = str_replace('<em>','',$text);
$text = str_replace('</em>','',$text);
Using PHP, given a string such as: this is a <strong>string</strong>; I need a function to strip out ALL html tags so that the output is: this is a string. Any ideas? Thanks in advance.
PHP has a built-in function that does exactly what you want: strip_tags
$text = '<b>Hello</b> World';
print strip_tags($text); // outputs Hello World
If you expect broken HTML, you are going to need to load it into a DOM parser and then extract the text.
What about using strip_tags, which should do just the job ?
For instance (quoting the doc) :
<?php
$text = '<p>Test paragraph.</p><!-- Comment --> Other text';
echo strip_tags($text);
echo "\n";
will give you :
Test paragraph. Other text
Edit : but note that strip_tags doesn't validate what you give it. Which means that this code :
$text = "this is <10 a test";
var_dump(strip_tags($text));
Will get you :
string 'this is ' (length=8)
(Everything after the thing that looks like a starting tag gets removed).
strip_tags is the function you're after. You'd use it something like this
$text = '<strong>Strong</strong>';
$text = strip_tags($text);
// Now $text = 'Strong'
I find this to be a little more effective than strip_tags() alone, since strip_tags() will not zap javascript or css:
$search = array(
"'<head[^>]*?>.*?</head>'si",
"'<script[^>]*?>.*?</script>'si",
"'<style[^>]*?>.*?</style>'si",
);
$replace = array("","","");
$text = strip_tags(preg_replace($search, $replace, $html));