Using PHP to remove a html element from a string - php

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);

Related

PHP : add a html tag around specifc words

I have a data base with texts and in each text there are words (tags) that start with # (example of a record : "Hi I'm posting an #issue on #Stackoverflow ")
I'm trying to find a solution to add html code to transform each tag into a link when printing the text.
So the text are stored as strings in MySQL database like this :
Some text #tag1 text #tag2 ...
I want to replace all these #abcd with
#abcd
And have a final result as follow:
Some text #tag1 text #tag2 ...
I guess that i should use some regex but it is not at all my strong side.
Try the following using preg_replace(..)
$input = "Hi I'm posting an #issue on #Stackoverflow";
echo preg_replace("/#([a-zA-Z0-9]+)/", "<a href='targetpage.php?val=$1'>#$1</a>", $input);
http://php.net/manual/en/function.preg-replace.php
A simple solution could look like this:
$re = '/\S*#(\[[^\]]+\]|\S+)/m';
$str = 'Some text #tag1 text #tag2 ...';
$subst = '#$1';
$result = preg_replace($re, $subst, $str);
echo "The result of the substitution is ".$result;
Demo
If you are actually after Twitter hashtags and want to go crazy take a look here how it is done in Java.
There is also a JavaScript Twitter library that makes things very easy.
Try this the function
<?php
$demoString1 = "THIS is #test STRING WITH #abcd";
$demoString2 = "Hi I'm posting an #issue on #Stackoverflow";
function wrapWithAnchor($link,$string){
$pattern = "/#([a-zA-Z0-9]+)/";
$replace_with = '<a href="'.$link.'?val=$1">$1<a>';
return preg_replace( $pattern, $replace_with ,$string );
}
$link= 'http://www.targetpage.php';
echo wrapWithAnchor($link,$demoString1);
echo '<hr />';
echo wrapWithAnchor($link,$demoString2);
?>

Strip just <p> tags in PHP

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>

substr doesn't work after using strip_tags

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);

PHP cut text from a specific word in an HTML string

I would like to cut every text ( image alt included ) in an HTML string form a specific word.
for example this is the string:
<?php
$string = '<div><img src="img.jpg" alt="cut this text form here" />cut this text form here</div>';
?>
and this is what I would like to output
<div>
<a href="#">
<img src="img.jpg" alt="cut this text" />
cut this text
</a>
</div>
The $string is actually an element of an Object but I didn't wanted to put too long code here.
Obviously I can't use explode because that would kill the HTML markup.
And also str_replace or substr is out because the length before or after the word where it needs to be cut is not constant.
So what can I do to achive this?
Ok I solved my problem and I only post an answer to my question because it could help someone.
so this is what I did:
<?php
$string = '<div><img src="img.jpg" alt="cut this text form here" />cut this text form here</div>';
$txt_only = strip_tags($string);
$explode = explode(' from', $txt_only);
$find_txt = array(' from', $explode[1]);
$new_str = str_replace($find_txt, '', $string);
echo $new_str;
?>
This might not be the best solution but it was quick and did not involve DOM Parse.
If anybody wants to try this make sure that your href or src or any ather attribute what needs to be untouched doesn't have any of the chars in the same way and order as in $find_txt else it will replace those too.

Remove characters in link within string

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

Categories