Im trying to put paragraph text around the paragraphs. This code pulls out the blockquotes from my Wordpress post and outputs everything else
html
<?php
$block2 = get_the_content();
$block2 = preg_replace('~<blockquote>([\s\S]+?)</blockquote>~', '', $block2);
echo '<p>'.$block2.'</p>';
?>
But it only puts < p > tags around the fist paragraph and not the others
If I've understood this correctly, you could try splitting $block2 by newlines, looping through the resulting array and wrapping each element of the array in <p> tags as you have done.
Currently, your code wraps the entire content of $block2 in <p> tags, where I assume you wanted it to wrap the sections separated by newlines.
Example (I don't remember the exact syntax for PHP - sorry):
$split_block = split($block2, '\n');
for ($i in $split_block) {
$split_block[$i] = '<p>'.$split_block[$i].'</p>';
}
echo $split_block;
I am trying to make "manner friendly" website. We use different declination dependent on gender and other factors. For example:
You did = robili
It did = robilo
She did = robila
Linguisticaly this is very simplified (and unlucky) example! I would like to change html text in php file where appropriate. For example
<? php
something
?>
html text of the page and somewhere is the word "robil"
<div>we tried to robil^i|o|a^</div>
<? php something ?>
Now I would like to replace all occurences of different tokens ^characters|characters|characters^ and replace them by one of their internal values according to "gender".
It is easy in javascript on the client side, but you will see all this weird "tokenizing" before javascript replace it.
Here I do not know the elegant solution.
Or do you have better idea?
Thanks for advice.
You can add these scripts before and after the HTML:
<?php
// start output buffering
ob_start();
?>
<html>
<body>
html text of the page and somewhere is the word "robil"
<div>we tried to robil^i|o|a^, but also vital^si|sa|ste^, borko^mal|mala|malo^ </div>
</body>
</html>
<?php
$use = 1; // indicate which declination to use (0,1 or 2)
// get buffered html
$html = ob_get_contents();
ob_end_clean();
// match anything between '^' than's not a control chr or '^', min 5 and max 20 chrs.
if (preg_match_all('/\^[^[:cntrl:]\^]{3,20}\^/',$html,$matches))
{
// replace all
foreach (array_unique($matches[0]) as $match)
{
$choices = explode('|',trim($match,'^'));
$html = str_replace($match,$choices[$use],$html);
}
}
echo $html;
This returns:
html text of the page and somewhere is the word "robil" we tried to
robilo, but also vitalsa, borkomala
This is a difficult question for me to word correctly, but I am trying to dynamically insert an ASIDE [specifically, just a "special thanks" note] between paragraphs. Initially I decided to drop this after the second paragraph by using substr_count(). I am floating this block, so if all it had to deal with was textual content there was no issue word-wrapping it. However, if it ran adjacent to an image or a PRE or anything else, it got wonky.
Anyway, what I want to do is detect when there is the first occurrence of TWO adjacent paragraphs and insert my aside between those. I.e.:
<p> Here is the first paragraph. </p>
<aside> INSERT THIS HERE </aside>
<p> Here is the second paragraph. </p>
Thoughts are appreciated.
Update: the substr_count() I am currently using.
Because I got voted down for not showing the original code, I'll post it below. I am using Wordpress but this isn't a WP specific question, as I'm taking the_content() as the string, counting the occurrences of P, and inserting the custom field there. This is ultimately not what I want to do, but I want to count two concurrent P's and insert this field between. It may be formatted strangely, as of course when I c/p from my editor stuff was all over the place.
$thanks = get_post_meta(get_the_ID(), 'thanks', true);
if (!$thanks) {
the_content();
}
else {
$show_after_p = 0;
$content = apply_filters('the_content', $post->post_content);
if(substr_count($content, '<p>') > $show_after_p) {
$contents = explode("</p>", $content);
$p_count = 0;
foreach($contents as $content)
{
echo $content;
if($p_count == $show_after_p)
{
?>
<aside class="thanks clearfix">
<p>
<?php echo '<span>Special thanks: </span>'.$thanks; ?>
</p>
</aside>
<?php }
echo "</p>";
$p_count++;
}
}
}
?>
Why not JS?
Well, javascript could definitely do this, but if I can do this in PHP then yahtzee.
use regular expression and preg_replace - search for: (closing of P){whitespaces (if any)}(opening P) and change whitespaces to your text.
something like that:
$content = preg_replace('#</p>\s*<p#', "</p><aside> INSERT THIS HERE </aside><p", $content);
Kudos to Jerzy Zawadzki for the inspiration. If you refer to the original code I segmented the content at the P tags using explode(). Now I am using preg_split() as follows:
$contents == preg_split('#</p>\s*<p>#', $content); // Thanks Jerzy
$i = 0;
foreach ( $contents as $content ) {
echo $content;
if ( $i == $show_after_p ) { // this is set to 0 right now
// insert aside here
}
echo "<p>";
$i++;
}
}
As far as I can tell, this busts up the content only at segments where there are concurrent paragraphs and no whitespace / images / other elements. At this first junction, I insert the aside. Then the content is appears as normal.
I'm currently having an issue with my website. I count like 150 words and then cut it for displaying as an intro text on my website but this produces an issue.
When we have something like this in the text:
<div>
////TEXT TEXT TEXT TEXT TEXT////
----> Reach 150 words here <------
////TEXT TEXT TEXT TEXT TEXT////
</div>
It will print this in the front-page:
<div>
////TEXT TEXT TEXT TEXT TEXT////
----> Reach 150 words here <------
and the unclosed <div> tag destroys the design as it is expected.
How can I overcome this issue? Can we like proccess unclosed tags and close them in the end?
Use php's strip_tags to remove the div from your copy, then add it back in afterwards.
For example;
<?php
$html = '<div>Content goes here</div>';
$stripped = strip_tags($html);
$excerpt_pos = strpos(' ', $stripped, 150);
?>
<div><?php echo substr($stripped, 0, $excerpt_pos); ?></div>
Looking for a regexp sequence of matches and replaces (preferably PHP but doesn't matter) to change this (the start and end is just random text that needs to be preserved).
IN:
fkdshfks khh fdsfsk
<!--g1-->
<div class='codetop'>CODE: AutoIt</div>
<div class='geshimain'>
<!--eg1-->
<div class="autoit" style="font-family:monospace;">
<span class="kw3">msgbox</span>
</div>
<!--gc2-->
<!--bXNnYm94-->
<!--egc2-->
<!--g2-->
</div>
<!--eg2-->
fdsfdskh
to this OUT:
fkdshfks khh fdsfsk
<div class='codetop'>CODE: AutoIt</div>
<div class='geshimain'>
<div class="autoit" style="font-family:monospace;">
<span class="kw3">msgbox</span>
</div>
</div>
fdsfdskh
Thanks.
Are you just trying to remove the comments? How about
s/<!--[^>]*-->//g
or the slightly better (suggested by the questioner himself):
<!--(.*?)-->
But remember, HTML is not regular, so using regular expressions to parse it will lead you into a world of hurt when somebody throws bizarre edge cases at it.
preg_replace('/<!--(.*)-->/Uis', '', $html)
This PHP code will remove all html comment tags from the $html string.
A better version would be:
(?=<!--)([\s\S]*?)-->
It matches html comments like these:
<!--
multi line html comment
-->
or
<!-- single line html comment -->
and what is most important it matches comments like this (the other regex shown by others do not cover this situation):
<!-- this is my blog: <mynixworld.inf> -->
Note
Although syntactically the one below is a html comment your browser might parse it somehow differently and thus it might have a special meaning. Stripping such strings might break your code.
<!--[if !(IE 8) ]><!-->
Do not forget to consider conditional comments, as
<!--(.*?)-->
will remove them. Try this instead:
<!--[^\[](.*?)-->
This will also remove downlevel-revealed conditional comments, though.
EDIT:
This won't remove downlevel-revealed or downlevel-hidden comments.
<!--(?!<!)[^\[>].*?-->
Ah I've done it,
<!--(.*?)-->
With next:
/( )*<!--((.*)|[^<]*|[^!]*|[^-]*|[^>]*)-->\n*/g
Can remove multiline comments using test string:
fkdshfks khh fdsfsk
<!--g1-->
<div class='codetop'>CODE: AutoIt</div>
<div class='geshimain'>
<!--eg1-->
<div class="autoit" style="font-family:monospace;">
<span class="kw3">msgbox</span>
</div>
<!--gc2-->
<!--bXNnYm94-->
<!--egc2-->
<!--g2-->
</div>
<!--eg2-->
fdsfdskh
<!-- --
> test
- -->
<!-- --
<- test <
>
- -->
<!--
test !<
- <!--
-->
<script type="text/javascript">//<![CDATA[
var xxx = 'a';
//]]></script>
ok
Try the following if your comments contain line breaks:
/<!--(.|\n)*?-->/g
<!--([\s\S]*?)-->
Works in javascript and VBScript also as "." doesn't match line breaks in all languages
Here is my attempt:
<!--(?!<!)[^\[>][\s\S]*?-->
This will also remove multi line comments and won't remove downlevel-revealed or downlevel-hidden comments.
I know that this is quite an old post, but I felt that it would be useful to add to this post in case anyone wants an easy to implement PHP function that directly answers the original question.
/**
* Strip all the html comments from $text
*
* #param $text - text to modify
* #param string $new replacement string
* #return array|string|string[]|null
*/
function strip_html_comments($text, $new=''){
$search = array ("|<!--[\s\S]*?-->|si");
$replace = array ($new);
return preg_replace($search, $replace, $text);
}
these code is also remove javascript code.
that's too bad :|
here's the example javascript code will be remove with this code:
<script type="text/javascript"><!--
var xxx = 'a';
//-->
</script>
function remove_html_comments($html) {
$expr = '/<!--[\s\S]*?-->/';
$func = 'rhc';
$html = preg_replace_callback($expr, $func, $html);
return $html;
}
function rhc($search) {
list($l) = $search;
if (mb_eregi("\[if",$l) || mb_eregi("\[endif",$l) ) {
return $l;
}
}
// Remove multiline comment
$mlcomment = '/\/\*(?!-)[\x00-\xff]*?\*\//';
$code = preg_replace ($mlcomment, "", $code);
// Remove single line comment
$slcomment = '/[^:]\/\/.*/';
$code = preg_replace ($slcomment, "", $code);
// Remove extra spaces
$extra_space = '/\s+/';
$code = preg_replace ($extra_space, " ", $code);
// Remove spaces that can be removed
$removable_space = '/\s?([\{\};\=\(\)\\\/\+\*-])\s?/';
$code = preg_replace ('/\s?([\{\};\=\(\)\/\+\*-])\s?/', "\\1", $code);
If you just want the text or text with specific tags you can handle this with PHP strip_tags it also delete HTML comment and you can save HTML tags you need like this:
$text = '<p>Test paragraph.</p><!-- Comment --> Other text';
echo strip_tags($text, ['p', 'a']);
the output will be:
<p>Test paragraph.</p> Other text
I hope it helps somebody!
You can achieve this with modern JavaScript.
function RemoveHtmlComments() {
let children = document.body.childNodes;
for (let child in children) {
if (children[child].nodeType === Node.COMMENT_NODE) children[child].remove();
}
}
It should be safer than RegEx.