Get content of first paragraph [duplicate] - php

This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 7 years ago.
I am working on a small php script I have an html code like this :
<p>text1</p> <p>text2</p> <p>text3</p>
Is it possible to use php to get only the content of the first paragraph which is text1 ?

Yes, is possible:
$text = '<p>text1</p> <p>text2</p> <p>text3</p>';
$text = preg_replace('/<\/p>[ ]{1,}<p>/', '</p><p>', $text);
$splitedText = explode('</p>', $text);
var_dump(substr($splitedText[0],3));
I Hope this helps!

Related

How to extract content from the quotation marks " MY TEXT " with PHP (preg_match_all) [duplicate]

This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 3 years ago.
I'm trying to extract some information out of a website.
My problem is that I need to extract text that is like this:
<p class="review"> "Desired text3" </p>
How to get the text that is inside the quotation marks " .... "?
Actually I've tried this but it wont work at all!
preg_match_all('|<p class=\"adress\">(.*)<\/p>|',$data,$text);
Any ideas?
<?php
$data = '<p class="review"> "Desired text3" </p>';
preg_match_all('|>\s*"(.*)"\s*<\/|',$data,$text);
var_dump($text[1][0]);
Demo

How to get string out of string [duplicate]

This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 5 years ago.
I have a long string and part of that string is this: <span itemprop="ratingValue">9.26</span>. I want to get 9.26 as a string (using explode?). Thanks for any help. PS: it is html code saved as string.
Using preg_match():
$input = 'verylongstring<withother>random</withother>
tags<span itemprop="ratingValue"><9.26</span>';
preg_match('~<span itemprop="ratingValue">(.*?)</span>~', $input, $output);
echo $output[1]; // 9.26

I want to scrape content or data from dynamic web page using php? [duplicate]

This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 6 years ago.
Is it possible to scrape content or data from dynamic web page? If it is possible please attach PHP code. Thanks in Advance.
<?php
$html = file_get_contents("http://yellowpages.sulekha.com/chennai/lakshmi-air-system-virugambakkam-chennai_contact-address");
preg_match_all(
'/ (.*?)</p>/s',
$html,
$posts,
PREG_SET_ORDER // formats data into an array of posts
);
print_r($posts[0][1]);
echo "" . count($posts) . " posts found";

Php nl2br show /r/n [duplicate]

This question already has answers here:
How to use nl2br() to handle string with '\r\n'?
(4 answers)
Closed 8 years ago.
When I use the function nl2br like result I see /r /n
example:
in a textarea I wrote
Hello,
how are you?,
fine
php
echo nl2br($_POST['message']);
result to screen
\r\nHello,
\r\nhow are you?
\r\nfin
how can I fix it? thank you
try this:
$newmessage = preg_replace('#(\\\r|\\\r\\\n|\\\n)#','<br />', $_POST['message']);

How to wrap a long text having no spaces in php [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to wrap long lines without spaces in HTML?
I have a long text "jkjkllllllllljcsccncnmchdkcjhvcjdkvk".how to wrap this text in php
Use this:
<?php
$iamlong = "woooooooooooord.";
$iamwrapped = wordwrap($iamlong , 8, "<br>", true);
echo $iamwrapped;
?>

Categories