0

I want to have the introtext and then the rest of the text in Hikashop product details page.

So in the product description I insert the <hr id="system-readmore" /> to separate the introtext and then in show_default.php:

  1. To show the introtext (at the top of product details page) I use this code with success:

    echo preg_replace('#<hr *id="system-readmore" */>.*#is','',$this->row->product_description);
    
  2. In the bottom of the same page, how can I show the rest of description? I mean the text that is after <hr id="system-readmore" />.

0

Your coding intent is to use <hr id="system-readmore" /> as your "string delimiter". For this type of task there are several functions that can lead you to the desired output. The most direct attack will be to call a function which will split your string in half then you can simply reference the two pieces.

Here is a demonstration of two splitting functions (take note of what happens with the possible leading/trailing whitespace characters):

Code: (Demo)

// this is your $this->row->product_description value for demonstration purposes:
$html = <<<HTML
Some text yatta yatta
<hr id="system-readmore" />
Some more text blah blah
HTML;

list($introtext, $description) = explode('<hr id="system-readmore" />', $html, 2);
var_dump($introtext);
var_dump($description);

echo "\n---\n";

var_dump(trim($introtext));  // this is tidier, with leading/trailing whitespace characters removed
var_dump(trim($description));  // this is tidier, with leading/trailing whitespace characters removed

echo "\n---\n";

list($introtext, $description) = preg_split('~\s*<hr id="system-readmore" />\s*~', $html, 2);
var_dump($introtext);  // no trim() call, no leading/trailing whitespace characters displayed
var_dump($description);  // no trim() call, no leading/trailing whitespace characters displayed

Output:

string(22) "Some text yatta yatta
"
string(25) "
Some more text blah blah"

---
string(21) "Some text yatta yatta"
string(24) "Some more text blah blah"

---
string(21) "Some text yatta yatta"
string(24) "Some more text blah blah"

Best coding practices, with a particular stress on efficiency/micro-optimization, states that you should only use regex if there is a particular advantage in doing so. If your delimiting string NEVER has whitespaces on either side, then you can use explode() without any trim() calls. If your delimiting string MAY have spaces, tabs, or newlines then I will recommend preg_split() because you receive 2 clean strings without any mopping up.

For your project, you would just use echo on the resulting variables. I used var_dump() to expose the whitespaces.

If you don't want to use list(). You can store the result of explode() as $halves and write echo $halves[0]; for the introtext and echo $halves[1]; for the description.

p.s. As I said, there are a few functions that will get your there. You could use preg_match() with ~^(.*?)\s*<hr id="system-readmore" />\s*(.*?)$~s (Demo) but this provides the unwanted fullstring match in its output. For future development with regex pattern, I highly endorse regex101.com because it not only highlights your matches in real time, it also gives an itemized breakdown of how your pattern is interpreted by the regex engine and offers the "step count" as a loose measure of pattern efficiency.

  • @geoplous is this answer helpful to you? Should I explain anything further or make any adjustments? – mickmackusa Jun 23 '18 at 2:53
  • @geoplous It seems that you have abandoned this page. Has may answer helped you? If this is answer has resolved your issue, please award my answer the green tick so that the system knows that this question is resolved. I have volunteered my time to help you, I would appreciate some acknowledgment of my effort. – mickmackusa Sep 30 '18 at 0:31

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.