PHP How to detect if paragraph contain multiple lines?
For example, I have this code:
<p>
"Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of
Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."
</p>
Where the correct would be:
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p>
<p>Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</p>
Is it possible to use xpath to do it?
How can I check every p tag, and if the case, format with the correct code?
php xpath
add a comment |
For example, I have this code:
<p>
"Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of
Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."
</p>
Where the correct would be:
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p>
<p>Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</p>
Is it possible to use xpath to do it?
How can I check every p tag, and if the case, format with the correct code?
php xpath
Are you scraping this from some website you don't control?
– ryantxr
Nov 24 '18 at 1:52
Yes, I am trying to format some posts I receive from rss feed.
– Migu3litto
Nov 24 '18 at 1:55
Tryexplode("n", $input)
then format the array elements.
– ryantxr
Nov 24 '18 at 1:57
@Migu3litto You mention an RSS feed in comments. You think that the related should be added? There could be relevance here and how it's formatted.
– Funk Forty Niner
Nov 24 '18 at 3:00
add a comment |
For example, I have this code:
<p>
"Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of
Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."
</p>
Where the correct would be:
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p>
<p>Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</p>
Is it possible to use xpath to do it?
How can I check every p tag, and if the case, format with the correct code?
php xpath
For example, I have this code:
<p>
"Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of
Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."
</p>
Where the correct would be:
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p>
<p>Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</p>
Is it possible to use xpath to do it?
How can I check every p tag, and if the case, format with the correct code?
php xpath
php xpath
asked Nov 24 '18 at 1:46
Migu3littoMigu3litto
315
315
Are you scraping this from some website you don't control?
– ryantxr
Nov 24 '18 at 1:52
Yes, I am trying to format some posts I receive from rss feed.
– Migu3litto
Nov 24 '18 at 1:55
Tryexplode("n", $input)
then format the array elements.
– ryantxr
Nov 24 '18 at 1:57
@Migu3litto You mention an RSS feed in comments. You think that the related should be added? There could be relevance here and how it's formatted.
– Funk Forty Niner
Nov 24 '18 at 3:00
add a comment |
Are you scraping this from some website you don't control?
– ryantxr
Nov 24 '18 at 1:52
Yes, I am trying to format some posts I receive from rss feed.
– Migu3litto
Nov 24 '18 at 1:55
Tryexplode("n", $input)
then format the array elements.
– ryantxr
Nov 24 '18 at 1:57
@Migu3litto You mention an RSS feed in comments. You think that the related should be added? There could be relevance here and how it's formatted.
– Funk Forty Niner
Nov 24 '18 at 3:00
Are you scraping this from some website you don't control?
– ryantxr
Nov 24 '18 at 1:52
Are you scraping this from some website you don't control?
– ryantxr
Nov 24 '18 at 1:52
Yes, I am trying to format some posts I receive from rss feed.
– Migu3litto
Nov 24 '18 at 1:55
Yes, I am trying to format some posts I receive from rss feed.
– Migu3litto
Nov 24 '18 at 1:55
Try
explode("n", $input)
then format the array elements.– ryantxr
Nov 24 '18 at 1:57
Try
explode("n", $input)
then format the array elements.– ryantxr
Nov 24 '18 at 1:57
@Migu3litto You mention an RSS feed in comments. You think that the related should be added? There could be relevance here and how it's formatted.
– Funk Forty Niner
Nov 24 '18 at 3:00
@Migu3litto You mention an RSS feed in comments. You think that the related should be added? There could be relevance here and how it's formatted.
– Funk Forty Niner
Nov 24 '18 at 3:00
add a comment |
3 Answers
3
active
oldest
votes
This code should do what you want. It uses DOMXPath
to find all the <p>
elements and then splits the content up into separate lines using preg_split
, replacing the content of the original <p>
element with the first line and then adding new <p>
elements as required for each of the subsequent lines.
$doc = new DOMDocument();
$doc->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$xpath = new DOMXpath($doc);
$paras = $xpath->query('//p');
foreach ($paras as $p) {
$lines = preg_split('/(s*[rn]s*)+/', $p->textContent, -1, PREG_SPLIT_NO_EMPTY);
$p->textContent = array_shift($lines);
foreach ($lines as $line) {
// create a new <p> element
$new = $doc->createElement('p');
$new->textContent = $line;
$p->parentNode->insertBefore($new, $p->nextSibling);
}
}
echo $doc->saveHTML();
Output for your sample data:
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p>
<p>Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
<p>It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of</p>
<p>Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</p>
Note that this code will only work when the <p>
element does not contain any child HTML elements (e.g. <a>
etc.). If that is the case the problem becomes a lot more complex...
Demo on 3v4l.org
add a comment |
I don't think you can do it with Xpath but here php example
<?php
$paragraph = <<<EOF
<p>
"Lorem Ipsum is simply dummy text.
Letraset sheets containing ."
</p>
EOF;
foreach(explode("n", $paragraph) as $line)
{
if(!empty($line) && strrpos($line, 'p>') === false)
echo "<p>" . trim($line, '"') . "</p>n";
}
add a comment |
A simple method is to use preg_split()
. After that echo them out wrapped in <p>
tag
Here are the example
PHP
<?php
// example code
$status = "Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of
Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
$tagsp = preg_split('/n+/', $status);
foreach($tagsp as $p)
{
if(strlen($p) > 0)
{
echo "<p>$p</p>";
}
}
DEMO
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53454505%2fphp-how-to-detect-if-paragraph-contain-multiple-lines%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
This code should do what you want. It uses DOMXPath
to find all the <p>
elements and then splits the content up into separate lines using preg_split
, replacing the content of the original <p>
element with the first line and then adding new <p>
elements as required for each of the subsequent lines.
$doc = new DOMDocument();
$doc->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$xpath = new DOMXpath($doc);
$paras = $xpath->query('//p');
foreach ($paras as $p) {
$lines = preg_split('/(s*[rn]s*)+/', $p->textContent, -1, PREG_SPLIT_NO_EMPTY);
$p->textContent = array_shift($lines);
foreach ($lines as $line) {
// create a new <p> element
$new = $doc->createElement('p');
$new->textContent = $line;
$p->parentNode->insertBefore($new, $p->nextSibling);
}
}
echo $doc->saveHTML();
Output for your sample data:
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p>
<p>Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
<p>It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of</p>
<p>Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</p>
Note that this code will only work when the <p>
element does not contain any child HTML elements (e.g. <a>
etc.). If that is the case the problem becomes a lot more complex...
Demo on 3v4l.org
add a comment |
This code should do what you want. It uses DOMXPath
to find all the <p>
elements and then splits the content up into separate lines using preg_split
, replacing the content of the original <p>
element with the first line and then adding new <p>
elements as required for each of the subsequent lines.
$doc = new DOMDocument();
$doc->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$xpath = new DOMXpath($doc);
$paras = $xpath->query('//p');
foreach ($paras as $p) {
$lines = preg_split('/(s*[rn]s*)+/', $p->textContent, -1, PREG_SPLIT_NO_EMPTY);
$p->textContent = array_shift($lines);
foreach ($lines as $line) {
// create a new <p> element
$new = $doc->createElement('p');
$new->textContent = $line;
$p->parentNode->insertBefore($new, $p->nextSibling);
}
}
echo $doc->saveHTML();
Output for your sample data:
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p>
<p>Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
<p>It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of</p>
<p>Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</p>
Note that this code will only work when the <p>
element does not contain any child HTML elements (e.g. <a>
etc.). If that is the case the problem becomes a lot more complex...
Demo on 3v4l.org
add a comment |
This code should do what you want. It uses DOMXPath
to find all the <p>
elements and then splits the content up into separate lines using preg_split
, replacing the content of the original <p>
element with the first line and then adding new <p>
elements as required for each of the subsequent lines.
$doc = new DOMDocument();
$doc->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$xpath = new DOMXpath($doc);
$paras = $xpath->query('//p');
foreach ($paras as $p) {
$lines = preg_split('/(s*[rn]s*)+/', $p->textContent, -1, PREG_SPLIT_NO_EMPTY);
$p->textContent = array_shift($lines);
foreach ($lines as $line) {
// create a new <p> element
$new = $doc->createElement('p');
$new->textContent = $line;
$p->parentNode->insertBefore($new, $p->nextSibling);
}
}
echo $doc->saveHTML();
Output for your sample data:
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p>
<p>Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
<p>It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of</p>
<p>Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</p>
Note that this code will only work when the <p>
element does not contain any child HTML elements (e.g. <a>
etc.). If that is the case the problem becomes a lot more complex...
Demo on 3v4l.org
This code should do what you want. It uses DOMXPath
to find all the <p>
elements and then splits the content up into separate lines using preg_split
, replacing the content of the original <p>
element with the first line and then adding new <p>
elements as required for each of the subsequent lines.
$doc = new DOMDocument();
$doc->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$xpath = new DOMXpath($doc);
$paras = $xpath->query('//p');
foreach ($paras as $p) {
$lines = preg_split('/(s*[rn]s*)+/', $p->textContent, -1, PREG_SPLIT_NO_EMPTY);
$p->textContent = array_shift($lines);
foreach ($lines as $line) {
// create a new <p> element
$new = $doc->createElement('p');
$new->textContent = $line;
$p->parentNode->insertBefore($new, $p->nextSibling);
}
}
echo $doc->saveHTML();
Output for your sample data:
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p>
<p>Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
<p>It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of</p>
<p>Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</p>
Note that this code will only work when the <p>
element does not contain any child HTML elements (e.g. <a>
etc.). If that is the case the problem becomes a lot more complex...
Demo on 3v4l.org
edited Nov 24 '18 at 3:11
answered Nov 24 '18 at 2:46
NickNick
31.9k121942
31.9k121942
add a comment |
add a comment |
I don't think you can do it with Xpath but here php example
<?php
$paragraph = <<<EOF
<p>
"Lorem Ipsum is simply dummy text.
Letraset sheets containing ."
</p>
EOF;
foreach(explode("n", $paragraph) as $line)
{
if(!empty($line) && strrpos($line, 'p>') === false)
echo "<p>" . trim($line, '"') . "</p>n";
}
add a comment |
I don't think you can do it with Xpath but here php example
<?php
$paragraph = <<<EOF
<p>
"Lorem Ipsum is simply dummy text.
Letraset sheets containing ."
</p>
EOF;
foreach(explode("n", $paragraph) as $line)
{
if(!empty($line) && strrpos($line, 'p>') === false)
echo "<p>" . trim($line, '"') . "</p>n";
}
add a comment |
I don't think you can do it with Xpath but here php example
<?php
$paragraph = <<<EOF
<p>
"Lorem Ipsum is simply dummy text.
Letraset sheets containing ."
</p>
EOF;
foreach(explode("n", $paragraph) as $line)
{
if(!empty($line) && strrpos($line, 'p>') === false)
echo "<p>" . trim($line, '"') . "</p>n";
}
I don't think you can do it with Xpath but here php example
<?php
$paragraph = <<<EOF
<p>
"Lorem Ipsum is simply dummy text.
Letraset sheets containing ."
</p>
EOF;
foreach(explode("n", $paragraph) as $line)
{
if(!empty($line) && strrpos($line, 'p>') === false)
echo "<p>" . trim($line, '"') . "</p>n";
}
answered Nov 24 '18 at 2:54
ewwinkewwink
11.9k22239
11.9k22239
add a comment |
add a comment |
A simple method is to use preg_split()
. After that echo them out wrapped in <p>
tag
Here are the example
PHP
<?php
// example code
$status = "Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of
Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
$tagsp = preg_split('/n+/', $status);
foreach($tagsp as $p)
{
if(strlen($p) > 0)
{
echo "<p>$p</p>";
}
}
DEMO
add a comment |
A simple method is to use preg_split()
. After that echo them out wrapped in <p>
tag
Here are the example
PHP
<?php
// example code
$status = "Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of
Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
$tagsp = preg_split('/n+/', $status);
foreach($tagsp as $p)
{
if(strlen($p) > 0)
{
echo "<p>$p</p>";
}
}
DEMO
add a comment |
A simple method is to use preg_split()
. After that echo them out wrapped in <p>
tag
Here are the example
PHP
<?php
// example code
$status = "Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of
Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
$tagsp = preg_split('/n+/', $status);
foreach($tagsp as $p)
{
if(strlen($p) > 0)
{
echo "<p>$p</p>";
}
}
DEMO
A simple method is to use preg_split()
. After that echo them out wrapped in <p>
tag
Here are the example
PHP
<?php
// example code
$status = "Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of
Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
$tagsp = preg_split('/n+/', $status);
foreach($tagsp as $p)
{
if(strlen($p) > 0)
{
echo "<p>$p</p>";
}
}
DEMO
answered Nov 24 '18 at 2:57
Fiido93Fiido93
1,4851918
1,4851918
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53454505%2fphp-how-to-detect-if-paragraph-contain-multiple-lines%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Are you scraping this from some website you don't control?
– ryantxr
Nov 24 '18 at 1:52
Yes, I am trying to format some posts I receive from rss feed.
– Migu3litto
Nov 24 '18 at 1:55
Try
explode("n", $input)
then format the array elements.– ryantxr
Nov 24 '18 at 1:57
@Migu3litto You mention an RSS feed in comments. You think that the related should be added? There could be relevance here and how it's formatted.
– Funk Forty Niner
Nov 24 '18 at 3:00