I have paragraph like this
<p>
<p>This is new content</p>
</p>
I want to remove that outer paragraph tag by using php.
how can we remove .can you please explain.
At face value, you can do
$content = preg_replace('~<p>\s*<p>(.*?)</p>\s*</p>~s','<p>$1</p>',$content);
here is a demo (demo has forwardslashes escaped since it uses / as delim)
.. but I have a sneaking suspicion your real content is more complex than this...
You can use str_replace function. This may not be the correct answer. But it works.
Try this
$string = "<p><p>This is new content</p></p>";
$string = str_replace("<p><p>","<p>",$string);
$string = str_replace("</p></p>","</p>",$string);
now $string will have "<p>This is new content</p>"
You can try this:
$str = preg_replace('!<p>(.*?)</p>!Uis', '$1', $str);
If you provide better context I can provide a better answer. This will take what's inside a pair of <p></p> tags and return it. If they are nested <p> tags then it will remove a level of nesting. If they arn't nested it will remove the <p> tag.
Replace spaces and line breaks between angle braces and then replace two paragraph tags with one tag.
$string = preg_replace('/>\s+</', '><', $string);
$string = str_replace("<p><p>","<p>",$string);
$string = str_replace("</p></p>","</p>",$string);
Related
I'm trying to replace some words between bracket with html tag. Thing I have is,
$string = "Please make it (yellow)! (Red) also ok. I think, you like (blue)";
Thing I want is,
$string = "Please make it <span class='font-bold'>(yellow)</span>! <span class='font-bold'>(Red)</span> also ok. I think, you like <span class='font-bold'>(blue)</span>";
If I would like to replace text not only in brackets, but also in something like "[text]","{text}",QtextQ, how can I do?
You'll want to become familiar with str_replace (http://php.net/manual/en/function.str-replace.php).
Here's how you would do it:
$pattern = ["(",")"];
$replace = ["<span class='bold-face'>(",")</span>"];
$newstring = str_replace($pattern, $replace, $string);
Also check out preg_replace (http://php.net/manual/en/function.preg-replace.php) for regular expression string replacements for even more flexibility.
Actually it's your task and you should try to solve it first before asking, and should provide what you have done so far.
Basically you have to find () with text in the middle, the best way is using regular expression pattern to find it.
Here solution using preg_replace
$string = 'Please make it (yellow)! (Red) also ok. I think, you like (blue)';
$pattern = '/(\(\w+\))/';
$replacement = '<span class=="font-bold=">${1}</span>';
echo preg_replace($pattern, $replacement, $string);
for [] {} QQ change pattern to this /(\(\w+\)|\[\w+\]|{\w+}|Q\w+Q)/
I am needing to change occurrence of string or numbers in php. In this situation, I need to change this, if it happens:
[code:154545edppy]
// my code here
[/code]
to this
[code]
// my code here
[/code]
I need to verify if letters and strings appear inside de opening block code. I am trying to do this with str_replace, but it's not working.
my code now:
$text = "[code:54as] [/code]";
$text = str_replace("[code: {(\d)}{(\w)}]", "[code]", $text);
$text = str_replace("[/code: {(\d)}{(\w)}]", "[/code]", $text);
echo $text;
str_replace is static. Use preg_replace with a regex and you can accomplish your task.
Something like:
$text = "[code:54as] [/code]";
echo preg_replace('~(\[/?.*?):.*?\]~', '$1]', $text);
Should do it.
PHP Demo: https://eval.in/643544
Regex demo: https://regex101.com/r/mD1bM3/1
If you only want to replace numbers and letters after the : use a character class in place of the second .*?. [A-Za-z\d]*?.
So I have an #mentions function on my site that users input themselves but can do something line:
#foo Hello This is some mention text included.
I would like to remove just the text (Everything after #foo) The content comes through the streamitem_content:
$json['streamitem_content_usertagged'] =
preg_replace('/(^|\s)#(\w+)/', '\1#$1',
$json['streamitem_content']);
Give this a try
$json['streamitem_content'] = '#foo Hello This is some mention text included.';
$json['streamitem_content_usertagged'] =
preg_replace('/#(\w+)/', '#$1',
$json['streamitem_content']);
echo $json['streamitem_content_usertagged'];
Output:
#foo Hello This is some mention text included.
Preg_replace will only replace what it finds so you don't need to find content you aren't interested. If you did want to capture multiple parts of a string though capture groups increase by one after each group (). So this
preg_replace('/(^|\s)#(\w+)/', '$1#$2',
$json['streamitem_content']);
echo $json['streamitem_content_usertagged'];
would actually be
preg_replace('/(^|\s)#(\w+)/', '$1#$2',
$json['streamitem_content']);
Update:
$json['streamitem_content'] = '#foo Hello This is some mention text included.';
$json['streamitem_content_usertagged'] =
preg_replace('/#(\w+).*$/', '#$1',
$json['streamitem_content']);
echo $json['streamitem_content_usertagged'];
Output:
#foo
If the content you want to replace after #foo can extended to multiple lines use the s modifier.
Regex101 Demo: https://regex101.com/r/tX1rO0/1
So pretty much the regex says find an # then capture all continuous a-zA-Z0-9_ characters. After a those continuos characters we don't care go to the end of the string.
You can use this:
preg_replace('/^\s*#(\w+)/', '#$1',
$json['streamitem_content']);
This removes the leading white space, and includes the # in the hyperlink's text (not the link argument).
If you need to keep the leading white space in tact:
preg_replace('/^(\s*)#(\w+)/', '$1#$2',
$json['streamitem_content']);
You could use explode(); and str_replace(); . They might have a speed advantage over preg.
Assuming the line is available as a variable (e.g. $mention):
$mention = $json['streamitem_content'];
$mention_parts = explode(" ", $mention);
$the_part_you_want = str_replace('#','', $mention_parts[0]);
// or you could use $the_part_you_want = ltrim($mention_parts[0], '#');
$json['streamitem_content_usertagged'] = '#' . $mention_parts[0] . '';
or use trim($mention_parts[0]); to remove any whitespace if it is unwanted.
You could use fewer variables and reuse $mention as array but this seemed a clearer way to illustrate the principle.
I expected this to be a simple regex but I guess my head isn't screwed on this morning!
I'm taking the source code of a page and tidying it up with a bunch of other preg_replaces, so by the time we get to the regex below, the result is already a single line string with things like comments stripped out, etc.
All I'm looking to do now is trim the texts between > and < char's down to remove extra whitespace. I.e.
<p> hello world </p>
should become
<p>hello world</p>
I figured this would do the trick, but it seems to do nothing?
$data = trim(preg_replace('/>(\s*)([^\s]*?)(\s*)</', '>$2<', $data));
Cheers.
Here's a ridiculous way to do it lol:
$str = "<p> hello world </p>";
$strArr = explode(" ", $str);
$strArr = array_filter($strArr);
var_dump(implode(" ",$strArr));
Use the power of arrays to remove the white spaces lol
you can use the /e modifier in regex to use the trim() function while replacing.
$data = preg_replace('/>([^<]*)</e', '">" . trim("$1") . "<"', $data);
A regex could be:
>\s+(.*[^\s])\s+<
but don't use it, there are better ways to reach that goal (example: HTMLtidy)
You may use this snippet of code.
$x = '<p> hello world </p>';
$foo = preg_replace('/>\s+/', '>', $x); //first remove space after ">" symbol
$foo = htmlentities(preg_replace('/\s+</', '<', $foo)); //now remove space before "<" symbol
echo $foo;
I have input Tags like this:
<input style="font-size:12px;width:100%" type="text" value="http://www.google.de/ggg">
And want to replace them with nothing.
This is, what I tried:
$pattern = '/<input style="font-size:12px;width:100%" type="text" value="(.+?)">/';
echo preg_replace( $pattern, "", $content )
I did not succeed with that.
What is the error in my function? Maybe the regex?
A function which replace all input tags inside the string would be fine.
Will replace all input tags whether they begin with white spaces. /i ignores case and /x ignores spaces in regexp so it primary purpose is more readable regexp,
echo preg_replace("/<\s* input [^>]+ >/xi", "", $content);
You probably need some pattern modifiers I am assuming you have a block of text so try adding sU after your pattern.
Using DOM objects is generally better for handling HTML than regexp.
$content = '<input style="font-size:12px;width:100%" type="text" value="asd"/>' ;
$dom = new DOMDocument ;
$dom->loadHTML($content);
$node = $dom->getElementsByTagName("input")->item(0);
$node->setAttribute("value", "");
echo $dom->saveHTML() ;
This also can explain why: RegEx match open tags except XHTML self-contained tags