I am trying to add a feature to our site that automatically fills the product short description by using first 2 paragraphs from long description if that product had no manually added short description.
My function worked in general, but we found out that some of our product texts start with headers ( <h2> up to <h5> )
.
My function checked if the first paragraph DOESN’T start with a ‘<h’ tag – then it posts two first paragraphs.
While if it does start with ‘<h’ tag – then I split the string.
I used $headerex = explode('</h2>', $long_description);
, which worked great, but only for splitting the string after tag. So if we had texts that start with H5 for instance – I would get nothing back.
Found out that explode does not work with regex(en?), so I tried using preg_split instead.
I am not familiar with all the nuances of regex and correctly using it, and I am really new to PHP.
$regexx = '</h[0-9]>';
$headerex = preg_split($regexx, $long_description);
When using this code, I split the string almost like I wanted to.
The problem is that the part with the header ends with printing "<" to the page after the header text.
When returning the second part that comes after the [ x = digit ] It starts with an ">".
I am missing either the regex or the preg_split.
I also tried $regexx = '</hd>
. they don’t work for me 🙁
if ( ! $short_description ) {
$short_descpars = explode('</p>', $long_description);
$short_descpar = $short_descpars[0];
if (strpos($short_descpar, '<h', 0) === false ){
$short_description = $short_descpar . " " . $short_descpars[1];
} else {
// $headerex = explode('</h2>', $long_description);
$regexx = '</h[0-9]>';
$headerex = preg_split($regexx, $long_description);
$headerex2 = $headerex[1];
$headerexx = explode('</p>', $headerex2);
$short_description = $headerexx[0] . " " . $headerexx[1];
}
?>
<div class="product_short_desc">
<p class="short_desc_header">
<?php echo $short_description; // WPCS: XSS ok. ?> </p>
<p class="long_desc_link">
<a href="#prod_full_desc"> קרא עוד על המוצר</a>
</p>
</div>
<?php
}
the deactivated code $headerex = explode('</h2>', $long_description);
worked, but only for h2, and I did not want to copy it for each header size.
And texts that started with h5 for instance – just missed the logic and showed nothing on frontend.
with </h[0-9]>
as regex with the preg_split the output text shows up like this:
The runaway < tag
2
Answers
I got overworked and made it way more complicated. I also realized that I just copied a snippet and never even tried to edit it to make it actually be useful in this situation.
Anyway, while I did not get the answers about the regex(en in plural?).
So eventually, to get the job done I've done the following:
Thank you for the help! If anything can be done better I'd love to know.
You can use the preg_split with the regex pattern /</?h[1-6]>/ to split the string by any header tag (from <h1> to <h6>). Then, it checks if the first segment starts with a header tag. If it does start with a header tag, it skips that segment and takes the next two segments as the short description. If it doesn’t, it takes the first two segments as the short description. You can use this function wherever you want to use the short description: