This question already has answers here:
replace space with new line
(2 answers)
Closed 7 years ago.
I have $str= "ns.kimsufi.com ks392904.kimsufi.com ks392904.kimsufi.com"
I want to have them like that as string :
$str= "ns.kimsufi.com
ks392904.kimsufi.com
ks392904.kimsufi.com"
What is the easiest way to do such thing in PHP ?
Problem didn't solved...
Here is whole code :
<?php
$str= 'res Athéna 2 rue Henri Bergson<br/>
<b>Tech City: </b>STRASBOURG<br/>
<b>Tech State/Province:<br/>
</b><b>Tech Postal Code: </b>67200<br/>
<b>Tech Country: </b>FR<br/>
<b>Tech Phone: </b>+33.679795486<br/>
<b>Tech Phone Ext:<br/>
</b><b>Tech Fax:<br/>
</b><b>Tech Fax Ext:<br/>
</b><b>Tech Email: </b>fnt25qgfilw16kj60goe#h.o-w-o.info<br/>
<b>Name Server: </b>ns.kimsufi.com<br/>
<b>Name Server: </b>ks392904.kimsufi.com<br/>
<b>Name Server: </b>ks392904.kimsufi.com<br/>
<b>DNSSEC: </b>unsigned<br/>
<b>URL of the ICANN WHOIS Data Problem Reporting System:<br/>
</b>http://wdprs.internic.net/<br/>
>>> Last update of WHOIS database: 2015-06-07T10:20:36.0Z <br />
</td></tr>
</table><br />
<form name="queryform" method="post" action="/index.php">
<table cellpadding="6" cellspacing="0" border="0" width="540" dir="ltr">
<tr><td bgcolor="#92CAFE">
<table width="100%" cellpadding="0" cellspacing="0" border="0" dir="ltr">
<tr class="upperrow">
<td align="left" valign="top" nowrap="nowrap"><font face="Arial" size="+0"><b>Enter any domain name:</b></font></td>
</tr>
<tr class="middlerow">
<td align="center" valign="middle" nowrap="nowrap"><input type="text" name="query" value="" class="queryinput" /> <input type="submit" name="submit" value="Check Domain" /></td>
</tr>
<tr class="lowerrow">
<td align="right" valign="bottom"></td>
</tr>
</table>';
$dom = new DOMDocument;
#$dom->loadHTML($str);
$xp = new DOMXPath($dom);
$links = $xp->query('//b[text()="Name Server: "]/following-sibling::a[1]');
foreach ($links as $link) {
$newlink = $link->nodeValue . PHP_EOL;
$newlink = str_replace(' ', "\n", $newlink);
echo $newlink;
}
?>
it still echo like that :
ns.kimsufi.com ks392904.kimsufi.com ks392904.kimsufi.com
what is the problem ?
if its not space between them what it is ?
any chance to change the code without using str_replace ?
You could use str_replace to do that.
$str = str_replace(" ", "\n", $str);
echo $str;
\n will do a linebreak.
Just replace space with newline character.
preg_replace('~\h~', '\n', $str);
str_replace alone would be enough for this job. But if you want to replace also the tabs, you must go with the above.
str_replace(' ', "\n", $str);
Related
This question already has answers here:
Send email with PHP from html form on submit with the same script
(8 answers)
Closed 5 years ago.
Hi im working on a button that will open a new mail tab with the predefined text but i cant have any paragraphs in the mail.
I tried \n and <br> nothing works...
$message .= '<html>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td>Annehmen →
</td>
</tr>
</table>
</td>
</tr>
</table>
</html>';
I understand your question now: you are passing the text through a URL, so you need to use urlencode() to preserve the newlines.
$body = urlencode('Servus, vielen Dank fuer deine Reservierungsanfrage, hiermit bestaetigen wir deine gewueschte Reservierung am ' . $date . ' um ' . $time . ' fuer ' . $amount . "\n" . ' Personen.');
Use \r\n instead of \n or <br>.
Also, if you specify the mail type as HTML, then <br> should work.
As per RFC2368 which defines mailto:, further reinforced by an example in RFC1738, it is explicitly stated that the only valid way to generate a line break is with %0D%0A.
Please try below code,
$message .= '<html>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td>Annehmen →
</td>
</tr>
</table>
</td>
</tr>
</table>
</html>';
this question too of stackoverflow,
enter link description here
I'am using strip_tags function to fetch only required content but it fetches the whole data from a link
see the example code below i m using to fetch content from a link:
<?php
$a=fopen("http://example.com/","r");
$contents=stream_get_contents($a);
fclose($a);
$contents1=strtolower($contents);
$start='<div id="content">';
$start_pos=strpos($contents1,$start);
$first_trim=substr($contents1,$start_pos);
$stop='</div><!-- content -->';
$stop_pos=strpos($first_trim,$stop);
$second_trim=substr($first_trim,0,$stop_pos+6);
$second_trim = strip_tags($second_trim, '<div><table><tbody><tr><td><a><h2><h4>');
echo "<div>$second_trim</div>";
?>
here is the html code fetched in $second_trim:
<div><div id="content">
<div id="issuedescription"></div>
<h2 class="wsite-content-title" style="text-align:center;">download content<br /><font color="#f30519">table of content</font><br /> <font color="#f80117"> content </font></h2>
<h2>table of contents</h2>
<h4 class="tocsectiontitle">editorial</h4>
<h2 class="wsite-content-title" style="text-align:left;">technical note</h2>
<table class="tocarticle" width="100%">
<tr valign="top">
<td class="toctitle" width="95%" align="left">where are we at and where are we heading to? </td>
<td class="tocgalleys" width="5%" align="left">
pdf
</td>
</tr>
<tr>
<td class="tocauthors" width="95%" align="left">
sergio eduardo de paiva gonã§alves </td>
<td class="tocpages" width="5%" align="left">1-2</td>
</tr>
</table>
<div class="separator"></div>
h4 class="tocsectiontitle">some text here</h4>
<table class="tocarticle" width="100%">
<tr valign="top">
<td class="toctitle" width="95%" align="left">some text here</td>
<td class="tocgalleys" width="5%" align="left">
pdf
</td>
</tr>
<tr>
<td class="tocauthors" width="95%" align="left">
some text here, some text here, some text here, some text here, some text here, some text here </td>
<td class="tocpages" width="5%" align="left">3-10</td>
</tr>
</table>
<a target="_blank" rel="license" href="http://example.com/">
</a>
some text here<a rel="license" target="_blank" href="http://example.com/">example</a>.
</div></div>
Now my problem is i want to fetch a particular tag only, from the whole content like 2nd anchor from two of given below using strip_tag function
pdf
some text here
and 2nd header tag from two of given below:
<h2 class="wsite-content-title" style="text-align:center;">download content<br /><font color="#f30519">table of content</font><br /> <font color="#f80117"> content </font></h2>
<h2>table of contents</h2>
but strip tag function is either fetching all of them or none of them , So how can i make them identify to fetch the tag I want instead of fetching all the similar tags.If their is any better way to do this please share your ideas here !!
A regexp can do such a thing:
function handle_link($data) {
list($link, $attributes, $content) = $data;
$classes = preg_match('#class=[\'"]([^\'"]+)[\'"]#', $attributes, $match) ? preg_split('#\s+#', $match[1]) : array();
// If the link has the "file" class
if(in_array('file', $classes)) {
return $content; // only the internal content (like strip_tags would do)
// or you can return a new link:
// return '' . $content . '';
} else {
return $link; // all the link not filtered
}
}
$second_trim = strip_tags($second_trim, '<div><table><tbody><tr><td><h2><h4>');
$second_trim = preg_replace_callback('#<a([^>]*)>(.+)</a>#U', 'handle_link', $second_trim);
I want to extract some data from a table using php preg_match_all(). I have the html as under,
I want to get the values in td,
say Product code: RC063154016.
How can I do that? I don'y have any experience with regex,
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td><span>Product code:</span> RC063154016</td>
<td><span>Gender:</span> Female</td>
</tr>
</tbody>
</table>
Use DomDocument
$str = <<<STR
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td><span>Product code:</span> RC063154016</td>
<td><span>Gender:</span> Female</td>
</tr>
</tbody>
</table>
STR;
$dom = new DOMDocument();
#$dom->loadHTML($str);
$tds = $dom->getElementsByTagName('td');
foreach($tds as $td){
echo $td->nodeValue . '<br>';
}
OUTPUT
Product code: RC063154016
Gender: Female
This should do for you:
preg_match_all('|<td><span>Product code:</span>([^<]*)</td>|', $html, $match);
But if you think there can be random white spaces around tags, then this one:
preg_match_all('|<td>\s*<span>\s*Product code:\s*</span>([^<]*)</td>|', $html, $match);
$data = <<<HTML
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td><span>Product code:</span> RC063154016</td>
<td><span>Gender:</span> Female</td>
</tr>
</tbody>
</table>
HTML;
if(preg_match_all('#<td>\s*<span>Product code:</span>\s*([^<]*)</td>#i', $data, $matches)) {
print_r($matches);
}
Use any one parser and parse the HTML and use it. Don't use preg* functions here. Please read this answer How do you parse and process HTML/XML in PHP?
I have a variable $html
in which this code is stored
<form action="track_mobile.asp" method="post" name="TrackMobile">
<table width="99%" height="55" border="0" cellpadding="2" cellspacing="0">
<tr>
<td class="Heading2" colspan="2"> Track Any Mobile Location</td>
</tr>
<tr>
<td width="21" valign="top" rowspan="2" class="s2stextbox" valign="top"><img src="../images/operators_logo/Tata.png" width="134" height="121" align="left"> </td>
<td width="897" valign="top" class="s2stextbox"><font size="2"><b>Mobile Number: </b>918888888888</font></td>
</tr>
<tr>
<td width="897" valign="top" class="s2stextbox" valign="top"><font size="2"> <b>User Name:</b> We are unable to trace the Name for this Mobile Number<br>
<font size="2"><b>Mobile Operator Name:</b> TATA TELESERVICES<br>
<b>State/Region: </b>Maharashtra</font></td>
</tr> </table>
</form>
In which every item after ":(semicollon)" are random which comes different every time.
Plz give me correct syntax to get echo as
Mobile Number: 918888888888
User Name: We are unable to trace the Name for this Mobile Number
Mobile Operator Name: IDEA
State/Region: Maharashtra
in which
these are ramdomly generated, different evey time so, that preg_match search this loaction and echo the text which are there in same loaction
918888888888
We are unable to trace the Name for this Mobile Number
IDEA
Maharashtra
1. Strip-out HTML tag
$text = strip_tags($html);
2. Match using text before column
I just change the regexp and display each value match by (.*) (everything that follow the pattern up to the end of line:
preg_match('/Mobile Number: (.*)/', $html, $matches);
echo $matches[1];
preg_match('/User Name: (.*)/', $html, $matches);
echo $matches[1];
preg_match('/Mobile Operator Name: (.*)/', $html, $matches);
echo $matches[1];
preg_match('/State/Region: (.*)/', $html, $matches);
echo $matches[1];
This is a work for XPath (see [SimpleXMLElement::xpath][1], your XPaths look like be:
918888888888
/form/table/tr[1]/td[2]/font[substring-after(./text(), ':') ->
We are unable to trace the Name for this Mobile Number
/form/table/tr[2]/td[1]/font[1][substring-after(./text(), ':')
TATA TELESERVICES
/form/table/tr[2]/td[1]/font[2][substring-before(substring-after(./text(), ':'), 'State')
Maharashtra
/form/table/tr[2]/td[1]/font[2][substring-after(./text(), 'Region')
I am currently trying to increase my knowledge of PHP and I have set myself the task of scraping a website and turning the data I retrieve into a JSON format.
Here is an example row of the data I am trying to parse:
<tr>
<td class="first">
<img id="ctl00_Content_ctl00_rptInfo_ctl01_Image1" alt="Active" src="../../images/t1.jpg" style="border-width:0px;" />
</td>
<td >
Copenhagen
</td>
<td>
Sas
</td>
<td>
SK537
</td>
<td>
02 Apr 10:20
</td>
<td class="last">
Delayed 11:30
</td>
</tr>
And here is my PHP code so far:
$raw = file_get_contents($url);
$newlines = array("\t","\n","\r","\x20\x20","\0","\x0B");
$content = str_replace($newlines, "", html_entity_decode($raw));
$start = strpos($content,'<table width="100%" cellspacing="0" cellpadding="0" border="0" summary="Departure times detail information"');
$end = strpos($content,'</table>',$start) + 8;
$table = substr($content,$start,$end-$start);
preg_match_all("|<tr(.*)</tr>|U",$table,$rows);
foreach ($rows[0] as $row){
if ((strpos($row,'<th')===false)){
preg_match_all("|<td(.*)</td>|U",$row,$cells);
$url_src = strip_tags($cells[0][0]);
$airport = strip_tags($cells[0][1]);
$airline = strip_tags($cells[0][2]);
$flightnum = strip_tags($cells[0][3]);
$schedule = strip_tags($cells[0][4]);
$status = strip_tags($cells[0][5]);
echo "{$url_src} - {$aiport} - {$airline} - {$flightnum} - {$schedule} - {$status}<br>\n";
}
}
I can currently get nearly all values correctly except I cannot seem to get anything for the cell that contains this:
<td class="first">
<img id="ctl00_Content_ctl00_rptInfo_ctl01_Image1" alt="Active" src="../../images/t1.jpg" style="border-width:0px;" />
</td>
Can anyone help me out with what I need to get the img string, I would be happy just being able to get the entire string within the <td></td> like this:
<img id="ctl00_Content_ctl00_rptInfo_ctl01_Image1" alt="Active" src="../../images/t1.jpg" style="border-width:0px;" />
But if its possible to parse out just the src string that would be very helpful.
Your <img> tag is not opening at all, that's why your regular expression won't parse it.
Try:
<td class="first">
<img id="ctl00_Content_ctl00_rptInfo_ctl01_Image1" alt="Active" src="../../images/t1.jpg" style="border-width:0px;" />
</td>