how to get some string between some symbol in php - php

I have php string like this :
$text = '<p>Artikel 20 November 2015,</p>
<p style="text-align: center;"><img src="../../../fileman/Uploads/Karangan Bunga Launching MD/Concert of Ari Lasso/Other/press conference/pers release/P_20151123_141352 - Copy 1.jpg" alt="" removed="" block="" auto="" margin-left:="" margin-right:="" width="723" height="1000" /></p>
<p style="text-align: left;">Artikel 21 November 2015,</p>
<p style="text-align: left;"><img src="../../../fileman/Uploads/Karangan Bunga Launching MD/Concert of Ari Lasso/Other/press conference/pers release/P_20151123_141312.jpg" alt="" style="display: block; margin-left: auto; margin-right: auto;" width="646" height="1000" /></p>';
I just want to take .jpg file from that string (ex: P_20151123_141352 - Copy 1.jpg & P_20151123_141312.jpg). I already try using preg_match_all but it did'nt work, maybe I just used wrong logic. Help me please :)

Related

HTML2PDF margin bug

I am currently using html2pdf to convert a document in my company to PHP, to be able to include my variables afterwards and save it as a pdf,
the problem is currently with my "div M1,M2,M3".
I make it align with "#M1,#M2,#M3{display: inline;}"
however I feel that my M2 "TEL:" is based on the top frame, rather than on the M1
$html = '<img src="logoTCSfinal.png" class="tcs" style="width:10%;margin-
left:20px;margin-right: 550px;">
<img src="ICSL.png" class="ics" style="width:10%;"><br>
<div style="width: 100%;padding-left:2px;padding-top:2px;font-weight: 600;padding-
bottom:2px;border: 3px solid #000000; text-align: left;">DEFINITION DES BESOINS</div>
<style>#M1,#M2,#M3{display: inline;}</style>
<div id="M1" style="padding-left:5px;padding-top: 10px;font-weight: 600;">SOCIETE:
</div>
<div id="M2" style="clear: both;font-weight: 600;">TEL:</div>
<div id="M3" style="font-weight: 600;">FAX:</div>';
$html2pdf = new Html2Pdf();
$html2pdf->writeHTML($html);
$html2pdf->output();
?>
but
however it displays it to me like
this

Need to extract the phone number within the string

I have a string like the following which can change every time.
style="margin: 0;">\r\n
Phone\r\n </p>\r\n <p style="font-weight: bold; margin:
0;">\r\n 0411313062\r\n </p>\r\n
</td>\r\n
</tr>\r\n
<tr>\r\n
<td style="padding-bottom: 18px;">\r\n
I need to extract the phone number 0411313062 out of this string.
You can try this:
$text = 'style="margin: 0;">\r\n
Phone\r\n </p>\r\n <p style="font-weight: bold; margin: 0;">\r\n 0411313062\r\n </p>\r\n
</td>\r\n
</tr>\r\n
<tr>\r\n
<td style="padding-bottom: 18px;">\r\n';
preg_match("/[0-9]{10}/", $text, $matches);// in case of multiple occurrences us preg_match_all
print_r($matches[0]);//0411313062
Try this
preg_match('\(?([0-9]{3})\s*\)?\s*-?\s*([0-9]{3})\s*-?\s*([0-9]{4})', $string, $match );
See it in action,
https://regex101.com/r/pLqDWw/3
This will match 7 or 10 digit phone numbers with or without the - or (area code) such as 800-555-5555 or 8005555555 or (800)555-5555 or 555-5555 etc
If you have to match more then one number, I would suggest using something like PHPQuery, to refine the text ( html ) you are searching for it against. You could try something simpler like, preg_match_all but once you get the hang of PHPQuery you'll thank me.
this code works fine. Try this
$tab = <<<EOD
style="margin: 0;">\r\n Phone\r\n </p>\r\n <p style="font-weight: bold; margin: 0;">\r\n 0411313062\r\n </p>\r\n </td>\r\n
</tr>\r\n
<tr>\r\n
<td style="padding-bottom: 18px;">\r\n) EOD;
$input= explode("\r\n", $tab);
print_r($input);
the phone number is
echo $input[4];

enable jquery slideToggle() work for multiple outputs that comes from a while

I have the following piece of code in php where I was trying to apply jquery slideToggle() for each youtube video presentation. My problem is that jquery slideToggle() works only for my first youtube video presentation inside my while statement. The rest of them are not working. Any idea how to fix this?
<style>
#flip
{
padding:0px;
}
#panel
{
width:475px;
margin-left:3px;
margin-bottom:5px;
padding:4px;
display:none;
box-shadow: 0 0 20px rgba(0, 30, 10, 2.8);
-webkit-box-shadow: 0 0 20px rgba(0, 10, 0, 2.8);
-moz-box-shadow: 0 0 20px rgba(0, 10, 0, 2.8);
background-color:#363636;
}
</style>
<script>
$(document).ready(function(){
$("#flip").click(function(){
$("#panel").slideToggle("slow");
});
});
</script>
<?php
// above code
//next starts the while and outputs from my table all rows that contains users posts
// my code outputs all youtube links in a presentation format
while(){
$row['comment'] = preg_replace("/\s*[a-zA-Z\/\/:\.]*youtube.com\/watch\?v=([a-zA-Z0-9\-_]+)([a-zA-Z0-9\/\*\-\_\?\&\;\%\=\.]*)/i", "</br>
<div id='panel'>
<object width=\"0px;\" height=\"0px;\">
<param name=\"movie\" value=\"http://www.youtube.com/v/$1&hl=en&fs=1\"></param>
<param name=\"allowFullScreen\" value=\"true\"></param>
<embed src=\"http://www.youtube.com/v/$1&hl=en&fs=1?&autoplay=1\" type=\"application/x-shockwave-flash\" allowfullscreen=\"true\" width=\"475px;\" height=\"260px;\"></embed>
</object>
</div>
<table style='border:1px solid lightgrey;'>
<td bgcolor='#EBEBEB' style='vertical-align:top;'>
<div id='flip' style='cursor:pointer;'>
<div id='container'><div id='img1'><img src='http://img.youtube.com/vi/$1/default.jpg'/></div><div id='img2'><img src='img/play-button.png' style='width:40px; height:40px;'/> </div></div>
</div>
</td>
<td width='355px;' bgcolor='#EBEBEB' style='vertical-align:top;'>
<font color='#363636'><b>$video_title</b></font><font color='#545454'></br><a href='http://www.youtube.com/watch?v={$id}' target='_blank'><font color='#69aa35'><b>Youtube.com</b></font></a> - $video_perigrafh...</font><font color='#949494'>[$view_count views]</font>
</td>
</table>", $row['comment']);
}
?>
You are using an id (#panel) and id's are supposed to be unique. The selector is based on the native GetElementByID, which returns the first match.
You should use a class (<div class="panel">) instead.
http://jsfiddle.net/gLXLr/ - illustration of the id vs class targeting
Edit: regarding the new code with classes, if your HTML is:
<div class='panel'>
...
</div>
<table style='border:1px solid lightgrey;'>
<tr>
<td bgcolor='#EBEBEB' style='vertical-align:top;'>
<div class='flip' style='cursor:pointer;'>
..
</div>
</td>
</tr>
</table>
The code for the toggle should be:
$(document).ready(function(){
$(".flip").click(function() {
$(this).parents('table').prev('.panel').slideToggle("slow");
});
});
You can adjust it to fit your needs , of course.

display a specific tag value from an html with php [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP: Strip a specific tag from HTML string?
How to parse and process HTML with PHP?
I have a description column on my mysql database which contains some html. like this:
<p><img src="http://www.somesite.co/uploads/Optimized-basil_rajapaksa.jpg" alt="" width="250" height="183" /></p>
<p><span style="font-size: medium; color: #0000ff;">neis,a" iriú weÿrka leojhs</span></p>
<p><span style="font-size: medium; color: #333333;">úYajúoHd, wdpd¾hjrekaf.a jD;a;Sh wr.,h iïnkaOfhka idlÉPd lsÍu ioyd wd¾:sl ixj¾Ok wud;H neis,a rdcmlaI uy;d iy úYajúoHd, wdpd¾hjrekaf.a iñ;s iïfï,kh w;r fyg ^6& Èk ;j;a idlÉPdjla meje;aùug ;SrKh ù ;sfnkjd'úYajúoHd, wdpd¾hjrekaf.a iñ;s iïfï</span><br /><span style="font-size: medium; color: #333333;">wod< idlÉPdjg wud;H neis,a rdcmlaI uy;df.ka ks< jYfhka werhqula ,enqK nj o Tyq i|yka l</span></p>
when i echo it on my web page its display the complete html but
i want to display only the first span tag value from this whole html on my web page
how it can be done?
Assuming you have your HTML code into a $html variable:
$html = <<<'CODE'
<p><img src="http://www.somesite.co/uploads/Optimized-basil_rajapaksa.jpg" alt="" width="250" height="183" /></p>
<p><span style="font-size: medium; color: #0000ff;">neis,a" iriú weÿrka leojhs</span></p>
<p><span style="font-size: medium; color: #333333;">úYajúoHd, wdpd¾hjrekaf.a jD;a;Sh wr.,h iïnkaOfhka idlÉPd lsÍu ioyd wd¾:sl ixj¾Ok wud;H neis,a rdcmlaI uy;d iy úYajúoHd, wdpd¾hjrekaf.a iñ;s iïfï,kh w;r fyg ^6& Èk ;j;a idlÉPdjla meje;aùug ;SrKh ù ;sfnkjd'úYajúoHd, wdpd¾hjrekaf.a iñ;s iïfï</span><br /><span style="font-size: medium; color: #333333;">wod< idlÉPdjg wud;H neis,a rdcmlaI uy;df.ka ks< jYfhka werhqula ,enqK nj o Tyq i|yka l</span></p>
CODE;
You can do this:
preg_match_all('/\<span[^\>]*\>([^\<]*)\<\/span\>/', $html, $matches);
echo $matches[1][0]; // Shows: neis,a" iriú weÿrka leojhs
And if you want the content of your second <span> just do:
echo $matches[1][1];
And so on in the case you had more <span> tags.
You should look into using DomDocument. It'll allow you to parse HTML with ease.

Problem splitting a string in PHP

My string looking like the below:
Harvard researchers develop platinum-free solid-oxide fuel cells, which could be reliable and cheap enough for mobile technology.
<br clear="both" style="clear: both;"/>
<br clear="both" style="clear: both;"/>
<a style='font-size: 10px; color: maroon;' href='http://www.pheedcontent.com/hostedMorselClick.php?hfmm=v3:4e19f3a5e1811fbd8d2609ab6e0b1790:xyHFPVLHAjBSu%2BHuriSZVqm9%2FODnAB81kZMY%2FW6XQhWC4ZbRzX%2BBHz7jOt1kjazUZT27efFh3vpwUMU%3D'><img border='0' title='Add to Twitter' alt='Add to Twitter' src='http://images.pheedo.com/images/mm/twitter.png'/></a>
<a style='font-size: 10px; color: maroon;' href='http://www.pheedcontent.com/hostedMorselClick.php?hfmm=v3:3e50e6b8d521eded6b35c7688aa906da:FrGGBNm1fSQsiuLmppzKM%2FATrKIoUDahb5X8uecXFxZVxeVzefUBbunDDSQIoM%2B7vZ%2FrMkI9MRbSJd0%3D'><img border='0' title='Add to Facebook' alt='Add to Facebook' src='http://images.pheedo.com/images/mm/facebook.gif'/></a>
<a style='font-size: 10px; color: maroon;' href='http://www.pheedcontent.com/hostedMorselClick.php?hfmm=v3:7e7e04c16c4c141c4117385690e52041:cBfF0Lt5lnF2klwL0yP1Z6C%2Bf6BV3FBNn1SMd9UUC1sTvBMcqqLi2LdjjD2Xx6LbCORRi%2F1sjoNWBYk%3D'><img border='0' title='Add to Slashdot' alt='Add to Slashdot' src='http://images.pheedo.com/images/mm/slashdot.png'/></a>
<a style='font-size: 10px; color: maroon;' href='http://www.pheedcontent.com/hostedMorselClick.php?hfmm=v3:abab99ee3bc19459ff3e8b7d8021840f:liR9O7Zfc0bI0Uuo10wyGIUoEOxlQXTWkWXuk6sb878dMYT2smVK1G5l0DxnIogEym5utExwYXrvUdM%3D'><img border='0' title='Add to digg' alt='Add to digg' src='http://images.pheedo.com/images/mm/digg_64x16.png'/></a>
<a style='font-size: 10px; color: maroon;' href='http://www.pheedcontent.com/hostedMorselClick.php?hfmm=v3:337479bf5b0ca164e90b9e6ee29a6bd2:Vo4lgAzFi7rA3OwEbIn3MCn8Wrc6ghW%2Bn8U%2FWvVnD%2FZAepXiRJLuKQ9jRNIB3tCaMfJBzkI0lN26WA%3D%3D'><img border='0' title='Add to del.icio.us' alt='Add to del.icio.us' src='http://images.pheedo.com/images/mm/delicious.gif'/></a>
<a style='font-size: 10px; color: maroon;' href='http://www.pheedcontent.com/hostedMorselClick.php?hfmm=v3:f1603669def1f797d29577bc6ffac6d8:L0FI9gIqTiSjo7LLh4IW%2FjEmU%2BevF%2Be%2B3Qh%2BEBIpZtBVoZeKf3mDbDWW%2FMjgIjP%2FujuheGGUDnffEyc%3D'><img border='0' title='Add to StumbleUpon' alt='Add to StumbleUpon' src='http://images.pheedo.com/images/mm/stumbleit.gif'/></a>
<a style='font-size: 10px; color: maroon;' href='http://www.pheedcontent.com/hostedMorselClick.php?hfmm=v3:6935f5f46c828b54e7f0a20ec855a0bc:31pmU19Ai%2BBD4P%2Bra8NUD3ywNzoeb%2B%2B%2F3UGXkY0dOTgERp7CGY8D%2FkhkfhSbDSkXYVynDgrcwPHJ3Q%3D%3D'><img border='0' title='Email this Article' alt='Email this Article' src='http://images.pheedo.com/images/mm/emailthis.png'/></a>
<br clear="both" style="clear: both;"/>
<img alt="" style="border: 0;" border="0" src="http://ads.pheedo.com/img.phdo?s=08f79e2459078baab633a35da651dfa4&p=1"/>
<img alt="" height="0" width="0" border="0" style="display:none" src="http://segment-pixel.invitemedia.com/pixel?code=TechBiz
&partnerID=167&key=segment"/><img alt="" height="0" width="0" border="0" style="display:none" src="http://pixel.quantserve.com/pixel/p-8bUhLiluj0fAw.gif?labels=pub.29821.rss.TechBiz
.15217,cat.TechBiz
.rss"/>
From the above string i only want the first line of it, ie:"Harvard researchers develop platinum-free solid-oxide fuel cells, which could be reliable and cheap enough for mobile technology."
Rest all is not required.
To do so, I have tried:
$description = split(' \n', $string);
$description = split('<', $string);
$description = split('. ', $string);
But none of them gives me the required result, which would be only the first line, without and spaces or lines after the full-stop.
Can some one plz help me with this.
Also, if someone could edit my text, as i am unable to paste my example string, as it actually is.
Regards
Zeeshan
In regex the dot character is a special character, you would need to escape it :
$description = split('\.', $string);
Also, split seems to be deprecated, you should be using preg_split: http://us.php.net/manual/en/function.preg-split.php
Something like:
$description = current(explode("\n", $string));
should work fine. Pay special attention to the double instead of single quotes.
If you're doing more of this kind of parsing, you should consider a HTML parser, such as a simplehtmldom.

Categories