How to remove text between tags in php?
$str = preg_replace('#(<a.*?>).*?(</a>)#', '$1$2', $str)
PHP Regex - Remove text between tags
You can use lazy quantifiers instead.
$s="foo<div>Some content is <b>bold</b>.</div>bar\n";
print preg_replace("/<div>.+?<\/div>/i", "", $s);'
output:foobar
UPDATE per comments:[ghoti@pc ~]$ cat doit.php
<?php
$text = 'text text text s html tagove
<div id="content"><b> stfu </b> ss adsda sdsa </div>
oshte text s html tagove';
print preg_replace('/<div id="content">.+?<\/div>/im', '', $text) . "\n";
[ghoti@pc ~]$ php doit.php
text text text s html tagove
oshte text s html tagove
[ghoti@pc ~]$
PHP regex - replace all text between a tag
As you're already using the capture groups, why not actually use them.
$link = "<a href='some-dynamic-link'>Text to replace</a>";
$newText = "Replaced!";
$result = preg_replace('/(<a.*?>).*?(<\/a>)/', '$1'.$newText.'$2', $link);
Remove content between HTML tags in PHP?
This solution uses regex. I will let you decide if it is complex or not.
$out = preg_replace("/(?<=^|>).*?(?=<|$)/s", "", $in);
Let's break it down:(?<=^|>)
: A lookbehind. Not actually matched, but it still has to be there. Matches either beginning of string (^
) or literal>
..*?
: Matches anything (s
modifier makes it include newline). The question mark makes it lazy - it matches as few characters as possible.(?=<|$)
: A lookahead. Matches either literal<
or end of string ($
).
""
), so that everything between >
and <
is deleted. A working demo can be seen here. It does not preserve whitespace, so you end up with one super long line.EDIT: If you know that your input will always be wrapped in HTML-tags you can make it even simpler for yourself, since you don't have to think about the beginning and end of string bits:
$out = preg_replace("/>.*?</s", "><", $in);
This variant will not work for input with text at the beginning or the end - for instance Hello <b>World</b>!
will become Hello<b></b>!
. PHP: Regex replace everything between to strings/HTML tags
The reason you're getting the error is because you've not escaped an opening [
character in your regular expression. Please see the [
I have marked below:
preg_replace('/\<p\>\[quote\]\<\/p\>[\s\S]+?\<p\>[\/quote\]\<\/p\>/', '', $string);
^
This has resulted in starting a character class that has not been closed. You should simply escape this opening brace like this:preg_replace('/\<p\>\[quote\]\<\/p\>[\s\S]+?\<p\>\[\/quote\]\<\/p\>/', '', $string);
remove tag but keep string between tag in php
Try this
$content = "
<script>
[random] string 1
</script>
<script>
[random] string 2
</script>
....
<script>
[random] string n
</script>
";
$content = str_replace(array("<script>", "</script>"), "", $content);
EDIT:Since you want to get rid of
<script></script>
and in the same time keep <script type="text/javascript"></script>
and because using regexp to solve this kind of problems is a bad idea then try to use the DOMDocument
like this:$dom = new DOMDocument();
$content = "
<script>
[random] string 1
</script>
<script>
[random] string 2
</script>
....
<script>
[random] string n
</script>
<script type='text/javascript'>
must keeping script
</script>
<script type='text/javascript'>
must keeping script
</script>
";
$dom->loadHTML($content);
$scripts = $dom->getElementsByTagName('script');
foreach ($scripts as $script) {
if (!$script->hasAttributes()) {
echo $script->nodeValue . "<br>";
}
}
This will output:[random] string 1
[random] string 2
[random] string n
Related Topics
Laravel: How to Use Multiple Pivot Table Relationships
How to Get Products from a Particular Category in Magento Ecommerce
Typecasting VS Function to Convert Variable Type in PHP
Laravel League/Flysystem Getting File Url with Aws S3
Convert Multidimensional Objects to Array
Cannot Unpack Array with String Keys
MySQL into Outfile Override Existing File
PHP Explode Array Then Loop Through Values and Output to Variable
Doctrine 2 Lifecyclecallbacks with Abstract Base Class Are Not Called
Woocommerce Cart Quantity Base Discount
Difference Between Directoryiterator and Filesystemiterator
Replace Any Url's Within a String of Text, to Clickable Links with PHP
Check and Return Duplicates Array PHP
Best Way to Get Hostname with PHP