📜 ⬆️ ⬇️

How to write your tag, analogue habracut? Simple and clear!

Situation. We write a site, among the possibilities of which is the possibility of creating your article, your news. News / Article should have a “Read More” button.

The problem . How to organize the division into a "quick view" / "Full view"?

Possible solutions. Create 2 input fields. In the first part of the article will be stored for “brief viewing”, in the second “for full”. Create for this 2 fields in the table ... I think it is not necessary.
')
Much better to create your own tag, which will share the article ...

I will explain in more detail.

Suppose we create an article that contains the text:
text1
text2
text3
text4
text5
text6
text7
text8

We need to hide the text from text3 to text6.
Enter your personal tag, for example: <projectname>

Now our text will look like this:
text1
text2
< projectname >text3
text4
text5
text6</ projectname >
text7
text8

Now you need to get the content among these tags ... Or replace the text in the tag with the "Read Text" button.

Get the text in tags in $ result [1]
preg_match("/<projectname>(.*)<\/projectname>/is", $text, $result);


Replace the text in the tags, on the inscription "read more"
preg_replace("/<projectname>(.*)<\/projectname>/is", "<a href="...#more">more</a>", $text);


In the article itself, we hide the tag and display the content ...
preg_replace("/<projectname>(.*)<\/projectname>/is", "<a name="more">$1</a>", $text);


Warning This is a greedy quantification, that is, the text "<tag> ABC </ tag> </ tag>" will be output as "ABC </ tag>"
For lazy quantification, you must replace (. *) With (. *?)

PS: In regular expressions, the modifier i indicates that the register is ignored, s indicates that line breaks are ignored!

Thank you all for your attention.

Crosspost

Source: https://habr.com/ru/post/56179/


All Articles