PHP preg_match_all run php function on matched items - php

I'm not sure of the terminology so I apologize ahead of time.
I'm trying to create a PHP template engine that will query a string for <ZONE header> and </ZONE header> and it will pull everything in between and then run a php function to see if the header exists. If the header exists it will display what was in between, and if the header does not exists it will remove what was in between.
Here's an example:
$string = "
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
<ZONE header><img src="images/header.jpg" /></ZONE header>
<p>Nam sollicitudin mattis nisi, eu convallis mi tincidunt vitae.</p>
";
The function would ideally remove <ZONE header><img src="images/header.jpg" /></ZONE header> then it will run the php function I've created header() which checks to see if the "header" exists in the database, and if it does, it will display everything inside <ZONE header></ZONE header> and if it doesn't it will remove it from the string.
If "header" exists:
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
<img src="images/header.jpg" />
<p>Nam sollicitudin mattis nisi, eu convallis mi tincidunt vitae.</p>
If "header" does not exist:
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
<p>Nam sollicitudin mattis nisi, eu convallis mi tincidunt vitae.</p>
Here is what I was working with but got stuck:
preg_match_all("|\<ZONE_header>(.*)\<\/ZONE_header>|isU", $string, $zone, PREG_SET_ORDER);
if (isset($zone) && is_array($zone)) {
foreach ($zone as $key => $zoneArray) {
if ($key == 0) {
$html = $zoneArray[1];
if ($html != "") {
if (header() != "") {
$html = str_replace($zoneArray[0], NULL, $html);
}
}
}
}
}
echo $html;
Any ideas, thoughts, suggestions?
Thank you for any and all help!

Note that I replace your header() function with get_header().
$string = preg_replace_callback('/<ZONE header>(.+)<\/ZONE header>/', 'replace_header', $string);
function replace_header($matches) {
return get_header() ? $matches[1] : '';
}
See documentation for preg_replace_callback.

Like this ?
$string = '
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
<ZONE header><img src="images/header.jpg" /></ZONE header>
<p>Nam sollicitudin mattis nisi, eu convallis mi tincidunt vitae.</p>
';
$pattern="#<ZONE header[^>]*>(.+)</ZONE header>#iU";
preg_match_all($pattern, $string, $matches);
if (strlen($matches[0][0])==0){
$string=strip_tags($string,"<p>");
}
else{
$string=strip_tags($string,"<p><img>");
}
echo $string;

Related

PHP - delete content after and before specific tag

I need to remove specific tags and text between them. For tags I can use str_replace but for text between I try using preg_replace but nothing works.
Code to fix:
// ...
[tabs_item title=”My title”] some different text here... [/tabs_item]
[tabs_item title=”My Other title”] some different text here... [/tabs_item]
// ...
my code:
$pattern = "/[tabs_item[^>]*title=\"my txt\"](.*?)[\\/tabs_item]/si";
$replacement = '';
$fix1 = preg_replace($pattern, $replacement, $content2);
$search = array('[/tabs_item]');
$replace = array('');
$fix2 = str_replace($search, $replace, $fix1);
return $fix2;
Thank you for posting full code. This needs to be handled a little differently since there is html in your content.
First, because your content contains the / char, we need to change the regex delimiter to something else so it doesn't interfere. I changed it to %. Next, since you're using a multi-line string, we need to set a modifier on the regex to include newline chars in the .(dot: all chars). This modifier is added by adding the 's' to the end of your pattern after the closing delimiter.
This code is tested and worked for me.
$content = '
<p>[tabs] [tabs_item title="Description"]<br />
Lorem ipsum dolor sit amet</p>
<p>Lorem ipsum dolor sit amet<br />
Lorem ipsum dolor sit amet<br />
Lorem ipsum dolor sit amet</p>
<p>[/tabs_item] [tabs_item title="Zwroty"]</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce nec mauris nunc. Vestibulum elit urna, aliquet et urna et, sagittis blandit ligula.</p>
[/tabs_item]
<p>[tabs_item title="Peyment and shipping"]</p>
<p>Lorem ipsum dolor sit amet</p>
<p><b>Lorem ipsum dolor sit amet</b>Lorem ipsum dolor sit amet</p>
<p>Lorem ipsum dolor sit amet</p>
<p>Lorem ipsum dolor sit amet</p>
<p>Lorem ipsum dolor sit amet</p>
<p>Lorem ipsum dolor sit amet " url"</p>
<p>[/tabs_item]</p>
<p>[/tabs]</p>';
$pattern = "%\[tabs_item[^\[]+\[/tabs_item\]%s";
$replacement = '';
$out = preg_replace($pattern, $replacement, $content);
echo $out;
Results in:
<p>[tabs]
<p></p>
<p>[/tabs]</p>
To answer your followup question: If you want to remove only this tag:
[tabs_item title="Peyment and shipping"]...[/tabs_item]
you can change your pattern to:
$pattern = '%\[tabs_item title="Peyment and shipping"\][^\[]+\[/tabs_item\]%s';
Which results in:
<p>[tabs] [tabs_item title="Description"]<br />
Lorem ipsum dolor sit amet</p>
<p>Lorem ipsum dolor sit amet<br />
Lorem ipsum dolor sit amet<br />
Lorem ipsum dolor sit amet</p>
<p>[/tabs_item] [tabs_item title="Zwroty"]</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce nec mauris nunc. Vestibulum elit urna, aliquet et urna et, sagittis blandit ligula.</p>
[/tabs_item]
<p></p>
<p>[/tabs]</p>
If you no have regexp skill you can make this with explode function like this;
<?php
$var = '// something
[tabs_item title=”My title”] some different text here... [/tabs_item]
[tabs_item title=”My Other title”] some different text here... [/tabs_item]
// something';
$first = explode('[tabs_item title=”My title”]',$var);
$start = $first[0];
$last = explode('[/tabs_item]',$first[1]);
$end = $last[count($last)-1];
echo $start.$end;
?>

Regex text and some tags between tag

I have problem with regex tag html. Any one please help me!
Thanks this is some case of me... I have search and think but not do it.
Case 1
// My input to regex
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit <br/><img src="img.jpg/> sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua<p>
// Out Put after regex
Lorem ipsum dolor sit amet, consectetur adipisicing elit <br/><img src="img.jpg/> sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua
Case 2
// My input to regex
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit</p>
// Out put after regex
Lorem ipsum dolor sit amet, consectetur adipisicing elit
Case 3
// My input to regex
<p><ul>...</ul><p>
//Out put after regex
NULL
I'm guessing something like this is what you're after (example in javascript).
function checkParagraph(str)
{
var result = str.match(/^<p>([^<].*[^>])<\/p>$/i);
if (result) return result[1];
else return null;
}
alert(checkParagraph("<p>Lorem ipsum <br/><img src=\"img.jpg\"/> magna aliqua</p>"));
alert(checkParagraph("<p>Lorem ipsum magna aliqua</p>"));
alert(checkParagraph("<p><img src=\"img.jpg\"/></p>"));
With the additional information about only allowing BR, IMG, A and IMG-inside-A tags, the regex is quite different:
function checkParagraph(str)
{
var result = str.match(/^<p>(([^<>]+|<br\/>|<img[^>]+>|<a[^>]+>[^<>]*<\/a>|<a[^>]+><img[^>]+><\/a>)*)<\/p>$/i);
if (result) return result[1];
else return null;
}
alert(checkParagraph("Lorem ipsum magna aliqua"));
alert(checkParagraph("<p>Lorem ipsum magna aliqua</p>"));
alert(checkParagraph("<p>Lorem ipsum <br/> magna aliqua</p>"));
alert(checkParagraph("<p>Lorem ipsum magna aliqua</p>"));
alert(checkParagraph("<p>Lorem ipsum <img src=\"img.jpg\"/> magna aliqua</p>"));
alert(checkParagraph("<p>Lorem ipsum <br/><img src=\"img.jpg\"/> magna aliqua</p>"));
alert(checkParagraph("<p><br/><img src=\"img.jpg\"/></p>"));
alert(checkParagraph("<p><span>magna</span> aliqua</p>"));
alert(checkParagraph("<p><span>magna</span> aliqua</p>"));
alert(checkParagraph("<p><br/><img src=\"img.jpg\"/><span>magna</span> aliqua</p>"));
Break-down of the regex:
/.../i -> case insensitive for upper and lower case tags
^<p>...<\/p>$ -> input is enclosed in P tag
(...) -> the capture group between the brackets will become result[1]
(...|...)* -> any number of the following options:
[^<>]+ -> option 1: any text without tags
<br\/> -> option 2: a BR tag
<img[^>]+> -> option 3: an IMG tag
<a[^>]+>[^<>]*<\/a> -> option 4: an A tag with text inside
<a[^>]+><img[^>]+><\/a> -> option 5: an A tag with an IMG tag inside

JSON format Invalid while doing json_encode in PHP

When i tried to display a JSON decoded response i got this JSON. But it returned invalid format.
I tried the same in http://www.jsoneditoronline.org/ and it also returned error.
Why is this error happening even if i tried to add addslashes() and htmlspecialchars function...
Any help is appreciated......
{"data":[{"id":"ZGRlBQR9ac3K","type":1,"title":"asasasas edieddddd","feed":{"is_img":0},"c_date":"2014-04-24 10:22:14","owner":"Joffin Joy","owner_first":"Joffin","owner_id":"ZGN0AQZ9ac3K","owner_type":"FH5R","photo":"http:\/\/localhost\/4thUser_v4\/images\/getuserphoto\/ZGN0AQZ9ac3K\/1\/small\/10443_1395738251_usr_crop.jpg","lc_cnt":[{"like":"1","comment":"6"}],"ckt_name":{"ZwVk":"Trainees"},"comments":{"ZwVk":[{"uc_id":"AQp1AN=9ac3K","uc_c_date":"2014-04-24 10:26:57","uc_m_date":"0000-00-00 00:00:00","comment":"dfdfdfdf","comment_type":"1","sender_type":"0","user_id":"ZGN0AQZ9ac3K","display_name":"Joffin Joy","u_type":"FH5R","photo":"http:\/\/localhost\/4thUser_v4\/images\/getuserphoto\/ZGN0AQZ9ac3K\/1\/small\/10443_1395738251_usr_crop.jpg","like_cnt":"1"}]}},{"id":"ZGNkZwN9ac3K","type":6,"title":"tlk","desc":"sdfsdfsdf","c_date":"2014-04-23 20:28:27","author_name":"Herald Charley","about_author":"sdfsdfsdf","authorId":"ZGNjAmD9ac3K","owner_first":"Sanesh","owner":"Sanesh Sunny","owner_id":"ZGNjBQN9ac3K","owner_type":"FH5R","photo":"http:\/\/localhost\/4thUser_v4\/images\/getuserphoto\/ZGNjBQN9ac3K\/1\/small\/noimage-ind.jpg","author_photo":"http:\/\/localhost\/4thUser_v4\/images\/getuserphoto\/ZGNjAmD9ac3K\/1\/small\/10074_1398168441_usr_lr.","lc_cnt":[{"like":"0","comment":"5"}],"ckt_name":{"ZwVk":"Trainees"},"comments":{"ZwVk":[{"uc_id":"AQp1BD=9ac3K","uc_c_date":"2014-04-24 19:23:15","uc_m_date":"0000-00-00 00:00:00","comment":"testing...","comment_type":"1","sender_type":"0","user_id":"ZGNkZwH9ac3K","display_name":"User Jolly","u_type":"FH5R","photo":"http:\/\/localhost\/4thUser_v4\/images\/getuserphoto\/ZGNkZwH9ac3K\/1\/small\/10125_1398517242_usr_crop.JPG","like_cnt":"0"},{"uc_id":"AQp2Aj=9ac3K","uc_c_date":"2014-04-25 12:48:41","uc_m_date":"0000-00-00 00:00:00","comment":"<script>alert('asdasdas')<\/script>","comment_type":"1","sender_type":"0","user_id":"ZGNkZwH9ac3K","display_name":"User Jolly","u_type":"FH5R","photo":"http:\/\/localhost\/4thUser_v4\/images\/getuserphoto\/ZGNkZwH9ac3K\/1\/small\/10125_1398517242_usr_crop.JPG","like_cnt":"0"},{"uc_id":"AQp3Aj=9ac3K","uc_c_date":"2014-04-27 19:45:05","uc_m_date":"2014-04-27 19:45:32","comment":"test comment123456789","comment_type":"1","sender_type":"0","user_id":"ZGNjAGx9ac3K","display_name":"Arungopan Gopakumar","u_type":"FH5R","photo":"http:\/\/localhost\/4thUser_v4\/images\/getuserphoto\/ZGNjAGx9ac3K\/1\/small\/10059_1398933445_usr_crop.jpg","like_cnt":"1"}]}},{"id":"ZGNlBGD9ac3K","type":7,"title":"jkhkhjk df sdfsdfdsfdsfsfdsf","source":"http:\/\/www.lipsum.com\/feed\/htmlhttp:\/\/www.lipsum.com\/feed\/htmlhttp:\/\/www.lipsum.com\/feed\/htmlhttp:\/\/www.lipsum.com\/feed\/htmlhttp:\/\/www.lipsum.com\/feed\/htmlhttp:\/\/www.lipsum.com\/feed\/html","c_date":"2014-04-22 12:53:17","summary":"","owner_first":"Joffin","owner":"Joffin Joy","owner_id":"ZGN0AQZ9ac3K","owner_type":"FH5R","photo":"http:\/\/localhost\/4thUser_v4\/images\/getuserphoto\/ZGN0AQZ9ac3K\/1\/small\/10443_1395738251_usr_crop.jpg","lc_cnt":[{"like":"0","comment":"7"}],"ckt_name":{"ZwVk":"Trainees"},"comments":{"ZwVk":[{"uc_id":"AQL2Aj=9ac3K","uc_c_date":"2014-04-22 13:07:42","uc_m_date":"0000-00-00 00:00:00","comment":"<script>alert('asdsadasdsa'"'''")<\/script>","comment_type":"1","sender_type":"0","user_id":"ZGNkZwH9ac3K","display_name":"User Jolly","u_type":"FH5R","photo":"http:\/\/localhost\/4thUser_v4\/images\/getuserphoto\/ZGNkZwH9ac3K\/1\/small\/10125_1398517242_usr_crop.JPG","like_cnt":"0"},{"uc_id":"AQL2BN=9ac3K","uc_c_date":"2014-04-22 13:08:09","uc_m_date":"0000-00-00 00:00:00","comment":"<script>alert('asdsadasdsa')<\/script>","comment_type":"1","sender_type":"0","user_id":"ZGNkZwH9ac3K","display_name":"User Jolly","u_type":"FH5R","photo":"http:\/\/localhost\/4thUser_v4\/images\/getuserphoto\/ZGNkZwH9ac3K\/1\/small\/10125_1398517242_usr_crop.JPG","like_cnt":"0"},{"uc_id":"AQL2BD=9ac3K","uc_c_date":"2014-04-22 13:10:06","uc_m_date":"0000-00-00 00:00:00","comment":"<script>alert('helloooo checking....');<\/script>","comment_type":"1","sender_type":"0","user_id":"ZGNkZwH9ac3K","display_name":"User Jolly","u_type":"FH5R","photo":"http:\/\/localhost\/4thUser_v4\/images\/getuserphoto\/ZGNkZwH9ac3K\/1\/small\/10125_1398517242_usr_crop.JPG","like_cnt":"0"}]}},{"id":"ZGRlZGL9ac3K","type":1,"title":"sample postApr 21 22014...edited sxdsdasdasdasdasdasdd\r\n\r\nLorem ipsum .","feed":{"is_img":0},"c_date":"2014-04-21 17:19:19","owner":"Joffin Joy","owner_first":"Joffin","owner_id":"ZGN0AQZ9ac3K","owner_type":"FH5R","photo":"http:\/\/localhost\/4thUser_v4\/images\/getuserphoto\/ZGN0AQZ9ac3K\/1\/small\/10443_1395738251_usr_crop.jpg","lc_cnt":null,"ckt_name":{"ZwVk":"Trainees"}},{"id":"ZGNlZGD9ac3K","type":2,"title":"sample event Apr 21 2014.....","desc":"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas adipiscing diam elit, ac pharetra magna rhoncus id. Quisque placerat imperdiet dolor in varius.","st_date":"Apr 21, 2014","st_time":"20:30:00","end_date":"May 14, 2014","end_time":"15:00:00","location":"Long Beach, CA, United States","c_date":"2014-04-21 17:15:10","owner_first":"Joffin","owner":"Joffin Joy","owner_id":"ZGN0AQZ9ac3K","owner_type":"FH5R","photo":"http:\/\/localhost\/4thUser_v4\/images\/getuserphoto\/ZGN0AQZ9ac3K\/1\/small\/10443_1395738251_usr_crop.jpg","lc_cnt":null,"ckt_name":{"ZwVk":"Trainees"}},{"id":"ZGNkBQx9ac3K","type":2,"title":"evnt on 17th april(edited)","desc":"description field\r\n~!##$%^&*()_+\/\\'"\u3053\u3093\u306b\u3061\u306fHallå\u4f60\u597d\u0928\u092e\u0938\u094d\u0924\u0947\r\nalert('hello");","st_date":"Jan 02, 2010","st_time":"10:05:00","end_date":"Apr 17, 2014","end_time":"10:05:00","location":"Mobile, AL, United States","c_date":"2014-04-17 10:05:28","owner_first":"Sanesh","owner":"Sanesh Sunny","owner_id":"ZGNjBQN9ac3K","owner_type":"FH5R","photo":"http:\/\/localhost\/4thUser_v4\/images\/getuserphoto\/ZGNjBQN9ac3K\/1\/small\/noimage-ind.jpg","lc_cnt":[{"like":"1","comment":"3"}],"ckt_name":{"ZwVk":"Trainees"},"comments":{"ZwVk":[{"uc_id":"AQLjZD=9ac3K","uc_c_date":"2014-04-17 11:07:42","uc_m_date":"0000-00-00 00:00:00","comment":"sdsds","comment_type":"1","sender_type":"0","user_id":"ZGR9ac3K","display_name":"User User Technologies Pvt Ltd","u_type":"D09F","photo":"http:\/\/localhost\/4thUser_v4\/images\/getuserphoto\/ZGR9ac3K\/1\/small\/1398519087_11.jpg","like_cnt":"0"},{"uc_id":"AQLkZN=9ac3K","uc_c_date":"2014-04-17 11:44:35","uc_m_date":"0000-00-00 00:00:00","comment":"sdfsdfsdfsdf","comment_type":"1","sender_type":"0","user_id":"ZGNjAmD9ac3K","display_name":"Herald Charley","u_type":"FH5R","photo":"http:\/\/localhost\/4thUser_v4\/images\/getuserphoto\/ZGNjAmD9ac3K\/1\/small\/10074_1398168441_usr_lr.","like_cnt":"0"},{"uc_id":"AQLkZD=9ac3K","uc_c_date":"2014-04-17 11:44:45","uc_m_date":"0000-00-00 00:00:00","comment":"sdfsdfsdfsdf","comment_type":"1","sender_type":"0","user_id":"ZGNjAmD9ac3K","display_name":"Herald Charley","u_type":"FH5R","photo":"http:\/\/localhost\/4thUser_v4\/images\/getuserphoto\/ZGNjAmD9ac3K\/1\/small\/10074_1398168441_usr_lr.","like_cnt":"0"}]}},{"id":"ZGRkAmN9ac3K","type":1,"title":"test post by sanesh on 16th april and shared with trainees ckt","feed":{"is_img":0},"c_date":"2014-04-16 14:10:56","owner":"Sanesh Sunny","owner_first":"Sanesh","owner_id":"ZGNjBQN9ac3K","owner_type":"FH5R","photo":"http:\/\/localhost\/4thUser_v4\/images\/getuserphoto\/ZGNjBQN9ac3K\/1\/small\/noimage-ind.jpg","lc_cnt":[{"like":"0","comment":"1"}],"ckt_name":{"ZwVk":"Trainees"}},{"id":"ZGRkAmD9ac3K","type":1,"title":"edited","feed":{"is_img":0},"c_date":"2014-04-16 14:21:23","owner":"Sanesh Sunny","owner_first":"Sanesh","owner_id":"ZGNjBQN9ac3K","owner_type":"FH5R","photo":"http:\/\/localhost\/4thUser_v4\/images\/getuserphoto\/ZGNjBQN9ac3K\/1\/small\/noimage-ind.jpg","lc_cnt":[{"like":"0","comment":"9"}],"ckt_name":{"ZwVk":"Trainees"},"comments":{"ZwVk":[{"uc_id":"AQH3BD=9ac3K","uc_c_date":"2014-04-16 15:31:41","uc_m_date":"0000-00-00 00:00:00","comment":"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam et malesuada libero. In hac habitasse platea dictumst. ","comment_type":"1","sender_type":"0","user_id":"ZGNjBQN9ac3K","display_name":"Sanesh Sunny","u_type":"FH5R","photo":"http:\/\/localhost\/4thUser_v4\/images\/getuserphoto\/ZGNjBQN9ac3K\/1\/small\/noimage-ind.jpg","like_cnt":"0"},{"uc_id":"AQH4Aj=9ac3K","uc_c_date":"2014-04-16 18:42:38","uc_m_date":"0000-00-00 00:00:00","comment":"~!##$%^&*()_+\/\\'"\u3053\u3093\u306b\u3061\u306fHallå\u4f60\u597d\u0928\u092e\u0938\u094d\u0924\u0947","comment_type":"1","sender_type":"0","user_id":"ZGNjBQN9ac3K","display_name":"Sanesh Sunny","u_type":"FH5R","photo":"http:\/\/localhost\/4thUser_v4\/images\/getuserphoto\/ZGNjBQN9ac3K\/1\/small\/noimage-ind.jpg","like_cnt":"0"}]}},{"id":"ZGNjAmx9ac3K","type":6,"title":"Sample Talk Added Apr 14 2014.....","desc":"Lorem ipsum dolor sit amet, consectetur adipiscing elit. In posuere elit odio, vel interdum ligula pretium vitae. Praesent eu orci non ipsum euismod mollis. ","c_date":"2014-04-14 18:34:44","author_name":"Sanesh Sunny","about_author":"Lorem ipsum dolor sit amet, consectetur adipiscing elit. In posuere elit odio,","authorId":"ZGNjBQN9ac3K","owner_first":"Joffin","owner":"Joffin Joy","owner_id":"ZGN0AQZ9ac3K","owner_type":"FH5R","photo":"http:\/\/localhost\/4thUser_v4\/images\/getuserphoto\/ZGN0AQZ9ac3K\/1\/small\/10443_1395738251_usr_crop.jpg","author_photo":"http:\/\/localhost\/4thUser_v4\/images\/getuserphoto\/ZGNjBQN9ac3K\/1\/small\/noimage-ind.jpg","lc_cnt":null,"ckt_name":{"ZwVk":"Trainees"}},{"id":"ZGRjAGR9ac3K","type":1,"title":"I amsdsdsdsdsd co-founder ofsdsdsds da","feed":{"is_img":0},"c_date":"2014-03-08 07:47:22","owner":"User Jolly","owner_first":"User","owner_id":"ZGNkZwH9ac3K","owner_type":"FH5R","photo":"http:\/\/localhost\/4thUser_v4\/images\/getuserphoto\/ZGNkZwH9ac3K\/1\/small\/10125_1398517242_usr_crop.JPG","lc_cnt":[{"like":"1","comment":"0"}],"ckt_name":{"ZGD3AQZ9ac3K":"Vijayee Bhava"}},{"id":"ZGRjAQx9ac3K","type":1,"title":"Lessons on Self Development\r\nhttp:\/\/localhost\/files\/vijayeebhava\/2\/Vijayeebhava_Lessons_Self_development.pptx\r\n\r\nPrepared by: Sreeja - Phoenix team","feed":{"is_img":0},"c_date":"2014-03-09 10:26:18","owner":"Vijayee Bhava","owner_first":"Vijayee Bhava","owner_id":"AQL2ZwL9ac3K","owner_type":"D09F","photo":"http:\/\/localhost\/4thUser_v4\/images\/getuserphoto\/AQL2ZwL9ac3K\/1\/small\/1394201730_46626.png","lc_cnt":[{"like":"1","comment":"0"}],"ckt_name":{"ZGD3AQH9ac3K":"Vijayee Bhava - II"}},{"id":"ZGNlAmL9ac3K","type":7,"title":"Key Lessons on LEADERSHIP","desc":"https:\/\/localhost\/files\/vijayeebhava\/2\/Vijayeebhava_Leadership_Key_Lessons.docx <\/a>","source":"","c_date":"2014-03-08 20:40:55","summary":"","owner_first":"Anaz","owner":"Anaz Kabeer","owner_id":"ZGNmZQH9ac3K","owner_type":"FH5R","photo":"http:\/\/localhost\/4thUser_v4\/images\/getuserphoto\/ZGNmZQH9ac3K\/1\/small\/10305_1382457349_usr_lr.","lc_cnt":[{"like":"1","comment":"0"}],"ckt_name":{"ZGD3AQH9ac3K":"Vijayee Bhava - II"}},{"id":"ZGNlAmp9ac3K","type":7,"title":"Qualities of an Entrepreneur","desc":"https:\/\/localhost\/files\/vijayeebhava\/2\/Vijayeebhava_Key_Lessons_Qualities_Entrepreneur.docx <\/a>","source":"","c_date":"2014-03-08 20:41:37","summary":"","owner_first":"Anaz","owner":"Anaz Kabeer","owner_id":"ZGNmZQH9ac3K","owner_type":"FH5R","photo":"http:\/\/localhost\/4thUser_v4\/images\/getuserphoto\/ZGNmZQH9ac3K\/1\/small\/10305_1382457349_usr_lr.","lc_cnt":[{"like":"1","comment":"0"}],"ckt_name":{"ZGD3AQH9ac3K":"Vijayee Bhava - II"}}],"tp_likes":"[id=\"tp_like_ZGNlAmL9ac3K_7_T\"],[id=\"tp_like_ZGNlAmp9ac3K_7_T\"],[id=\"tp_like_ZGRjAQx9ac3K_1_T\"],[id=\"tp_like_ZGRlBQR9ac3K_1_T\"]","cmnt_likes":"[id=\"tp_like_ZGRlBQR9ac3K_1_AQp1AN=9ac3K_C\"]","up_next":"2014-03-09 09:14:44","up_start":0,"up_end":"15"}
There is a lot of Javascript in that JSON, and the double quotes in the alert()'s are whats making it invalid.
Paste your string in there, it shows you everything that's invalid
http://jsonformatter.curiousconcept.com/#jsonformatter
For reference I removed all the invalid parts, this string is valid JSON
{"data":[{"id":"ZGRlBQR9ac3K","type":1,"title":"asasasas edieddddd","feed":{"is_img":0},"c_date":"2014-04-24 10:22:14","owner":"Joffin Joy","owner_first":"Joffin","owner_id":"ZGN0AQZ9ac3K","owner_type":"FH5R","photo":"http:\/\/localhost\/4thUser_v4\/images\/getuserphoto\/ZGN0AQZ9ac3K\/1\/small\/10443_1395738251_usr_crop.jpg","lc_cnt":[{"like":"1","comment":"6"}],"ckt_name":{"ZwVk":"Trainees"},"comments":{"ZwVk":[{"uc_id":"AQp1AN=9ac3K","uc_c_date":"2014-04-24 10:26:57","uc_m_date":"0000-00-00 00:00:00","comment":"dfdfdfdf","comment_type":"1","sender_type":"0","user_id":"ZGN0AQZ9ac3K","display_name":"Joffin Joy","u_type":"FH5R","photo":"http:\/\/localhost\/4thUser_v4\/images\/getuserphoto\/ZGN0AQZ9ac3K\/1\/small\/10443_1395738251_usr_crop.jpg","like_cnt":"1"}]}},{"id":"ZGNkZwN9ac3K","type":6,"title":"tlk","desc":"sdfsdfsdf","c_date":"2014-04-23 20:28:27","author_name":"Herald Charley","about_author":"sdfsdfsdf","authorId":"ZGNjAmD9ac3K","owner_first":"Sanesh","owner":"Sanesh Sunny","owner_id":"ZGNjBQN9ac3K","owner_type":"FH5R","photo":"http:\/\/localhost\/4thUser_v4\/images\/getuserphoto\/ZGNjBQN9ac3K\/1\/small\/noimage-ind.jpg","author_photo":"http:\/\/localhost\/4thUser_v4\/images\/getuserphoto\/ZGNjAmD9ac3K\/1\/small\/10074_1398168441_usr_lr.","lc_cnt":[{"like":"0","comment":"5"}],"ckt_name":{"ZwVk":"Trainees"},"comments":{"ZwVk":[{"uc_id":"AQp1BD=9ac3K","uc_c_date":"2014-04-24 19:23:15","uc_m_date":"0000-00-00 00:00:00","comment":"testing...","comment_type":"1","sender_type":"0","user_id":"ZGNkZwH9ac3K","display_name":"User Jolly","u_type":"FH5R","photo":"http:\/\/localhost\/4thUser_v4\/images\/getuserphoto\/ZGNkZwH9ac3K\/1\/small\/10125_1398517242_usr_crop.JPG","like_cnt":"0"},{"uc_id":"AQp2Aj=9ac3K","uc_c_date":"2014-04-25 12:48:41","uc_m_date":"0000-00-00 00:00:00","comment":"","comment_type":"1","sender_type":"0","user_id":"ZGNkZwH9ac3K","display_name":"User Jolly","u_type":"FH5R","photo":"http:\/\/localhost\/4thUser_v4\/images\/getuserphoto\/ZGNkZwH9ac3K\/1\/small\/10125_1398517242_usr_crop.JPG","like_cnt":"0"},{"uc_id":"AQp3Aj=9ac3K","uc_c_date":"2014-04-27 19:45:05","uc_m_date":"2014-04-27 19:45:32","comment":"test comment123456789","comment_type":"1","sender_type":"0","user_id":"ZGNjAGx9ac3K","display_name":"Arungopan Gopakumar","u_type":"FH5R","photo":"http:\/\/localhost\/4thUser_v4\/images\/getuserphoto\/ZGNjAGx9ac3K\/1\/small\/10059_1398933445_usr_crop.jpg","like_cnt":"1"}]}},{"id":"ZGNlBGD9ac3K","type":7,"title":"jkhkhjk df sdfsdfdsfdsfsfdsf","source":"http:\/\/www.lipsum.com\/feed\/htmlhttp:\/\/www.lipsum.com\/feed\/htmlhttp:\/\/www.lipsum.com\/feed\/htmlhttp:\/\/www.lipsum.com\/feed\/htmlhttp:\/\/www.lipsum.com\/feed\/htmlhttp:\/\/www.lipsum.com\/feed\/html","c_date":"2014-04-22 12:53:17","summary":"","owner_first":"Joffin","owner":"Joffin Joy","owner_id":"ZGN0AQZ9ac3K","owner_type":"FH5R","photo":"http:\/\/localhost\/4thUser_v4\/images\/getuserphoto\/ZGN0AQZ9ac3K\/1\/small\/10443_1395738251_usr_crop.jpg","lc_cnt":[{"like":"0","comment":"7"}],"ckt_name":{"ZwVk":"Trainees"},"comments":{"ZwVk":[{"uc_id":"AQL2Aj=9ac3K","uc_c_date":"2014-04-22 13:07:42","uc_m_date":"0000-00-00 00:00:00","comment":"","comment_type":"1","sender_type":"0","user_id":"ZGNkZwH9ac3K","display_name":"User Jolly","u_type":"FH5R","photo":"http:\/\/localhost\/4thUser_v4\/images\/getuserphoto\/ZGNkZwH9ac3K\/1\/small\/10125_1398517242_usr_crop.JPG","like_cnt":"0"},{"uc_id":"AQL2BN=9ac3K","uc_c_date":"2014-04-22 13:08:09","uc_m_date":"0000-00-00 00:00:00","comment":"","comment_type":"1","sender_type":"0","user_id":"ZGNkZwH9ac3K","display_name":"User Jolly","u_type":"FH5R","photo":"http:\/\/localhost\/4thUser_v4\/images\/getuserphoto\/ZGNkZwH9ac3K\/1\/small\/10125_1398517242_usr_crop.JPG","like_cnt":"0"},{"uc_id":"AQL2BD=9ac3K","uc_c_date":"2014-04-22 13:10:06","uc_m_date":"0000-00-00 00:00:00","comment":"","comment_type":"1","sender_type":"0","user_id":"ZGNkZwH9ac3K","display_name":"User Jolly","u_type":"FH5R","photo":"http:\/\/localhost\/4thUser_v4\/images\/getuserphoto\/ZGNkZwH9ac3K\/1\/small\/10125_1398517242_usr_crop.JPG","like_cnt":"0"}]}},{"id":"ZGRlZGL9ac3K","type":1,"title":"sample postApr 21 22014...edited sxdsdasdasdasdasdasdd\r\n\r\nLorem ipsum .","feed":{"is_img":0},"c_date":"2014-04-21 17:19:19","owner":"Joffin Joy","owner_first":"Joffin","owner_id":"ZGN0AQZ9ac3K","owner_type":"FH5R","photo":"http:\/\/localhost\/4thUser_v4\/images\/getuserphoto\/ZGN0AQZ9ac3K\/1\/small\/10443_1395738251_usr_crop.jpg","lc_cnt":null,"ckt_name":{"ZwVk":"Trainees"}},{"id":"ZGNlZGD9ac3K","type":2,"title":"sample event Apr 21 2014.....","desc":"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas adipiscing diam elit, ac pharetra magna rhoncus id. Quisque placerat imperdiet dolor in varius.","st_date":"Apr 21, 2014","st_time":"20:30:00","end_date":"May 14, 2014","end_time":"15:00:00","location":"Long Beach, CA, United States","c_date":"2014-04-21 17:15:10","owner_first":"Joffin","owner":"Joffin Joy","owner_id":"ZGN0AQZ9ac3K","owner_type":"FH5R","photo":"http:\/\/localhost\/4thUser_v4\/images\/getuserphoto\/ZGN0AQZ9ac3K\/1\/small\/10443_1395738251_usr_crop.jpg","lc_cnt":null,"ckt_name":{"ZwVk":"Trainees"}},{"id":"ZGNkBQx9ac3K","type":2,"title":"evnt on 17th april(edited)","desc":"","st_date":"Jan 02, 2010","st_time":"10:05:00","end_date":"Apr 17, 2014","end_time":"10:05:00","location":"Mobile, AL, United States","c_date":"2014-04-17 10:05:28","owner_first":"Sanesh","owner":"Sanesh Sunny","owner_id":"ZGNjBQN9ac3K","owner_type":"FH5R","photo":"http:\/\/localhost\/4thUser_v4\/images\/getuserphoto\/ZGNjBQN9ac3K\/1\/small\/noimage-ind.jpg","lc_cnt":[{"like":"1","comment":"3"}],"ckt_name":{"ZwVk":"Trainees"},"comments":{"ZwVk":[{"uc_id":"AQLjZD=9ac3K","uc_c_date":"2014-04-17 11:07:42","uc_m_date":"0000-00-00 00:00:00","comment":"sdsds","comment_type":"1","sender_type":"0","user_id":"ZGR9ac3K","display_name":"User User Technologies Pvt Ltd","u_type":"D09F","photo":"http:\/\/localhost\/4thUser_v4\/images\/getuserphoto\/ZGR9ac3K\/1\/small\/1398519087_11.jpg","like_cnt":"0"},{"uc_id":"AQLkZN=9ac3K","uc_c_date":"2014-04-17 11:44:35","uc_m_date":"0000-00-00 00:00:00","comment":"sdfsdfsdfsdf","comment_type":"1","sender_type":"0","user_id":"ZGNjAmD9ac3K","display_name":"Herald Charley","u_type":"FH5R","photo":"http:\/\/localhost\/4thUser_v4\/images\/getuserphoto\/ZGNjAmD9ac3K\/1\/small\/10074_1398168441_usr_lr.","like_cnt":"0"},{"uc_id":"AQLkZD=9ac3K","uc_c_date":"2014-04-17 11:44:45","uc_m_date":"0000-00-00 00:00:00","comment":"sdfsdfsdfsdf","comment_type":"1","sender_type":"0","user_id":"ZGNjAmD9ac3K","display_name":"Herald Charley","u_type":"FH5R","photo":"http:\/\/localhost\/4thUser_v4\/images\/getuserphoto\/ZGNjAmD9ac3K\/1\/small\/10074_1398168441_usr_lr.","like_cnt":"0"}]}},{"id":"ZGRkAmN9ac3K","type":1,"title":"test post by sanesh on 16th april and shared with trainees ckt","feed":{"is_img":0},"c_date":"2014-04-16 14:10:56","owner":"Sanesh Sunny","owner_first":"Sanesh","owner_id":"ZGNjBQN9ac3K","owner_type":"FH5R","photo":"http:\/\/localhost\/4thUser_v4\/images\/getuserphoto\/ZGNjBQN9ac3K\/1\/small\/noimage-ind.jpg","lc_cnt":[{"like":"0","comment":"1"}],"ckt_name":{"ZwVk":"Trainees"}},{"id":"ZGRkAmD9ac3K","type":1,"title":"edited","feed":{"is_img":0},"c_date":"2014-04-16 14:21:23","owner":"Sanesh Sunny","owner_first":"Sanesh","owner_id":"ZGNjBQN9ac3K","owner_type":"FH5R","photo":"http:\/\/localhost\/4thUser_v4\/images\/getuserphoto\/ZGNjBQN9ac3K\/1\/small\/noimage-ind.jpg","lc_cnt":[{"like":"0","comment":"9"}],"ckt_name":{"ZwVk":"Trainees"},"comments":{"ZwVk":[{"uc_id":"AQH3BD=9ac3K","uc_c_date":"2014-04-16 15:31:41","uc_m_date":"0000-00-00 00:00:00","comment":"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam et malesuada libero. In hac habitasse platea dictumst. ","comment_type":"1","sender_type":"0","user_id":"ZGNjBQN9ac3K","display_name":"Sanesh Sunny","u_type":"FH5R","photo":"http:\/\/localhost\/4thUser_v4\/images\/getuserphoto\/ZGNjBQN9ac3K\/1\/small\/noimage-ind.jpg","like_cnt":"0"},{"uc_id":"AQH4Aj=9ac3K","uc_c_date":"2014-04-16 18:42:38","uc_m_date":"0000-00-00 00:00:00","comment":"","comment_type":"1","sender_type":"0","user_id":"ZGNjBQN9ac3K","display_name":"Sanesh Sunny","u_type":"FH5R","photo":"http:\/\/localhost\/4thUser_v4\/images\/getuserphoto\/ZGNjBQN9ac3K\/1\/small\/noimage-ind.jpg","like_cnt":"0"}]}},{"id":"ZGNjAmx9ac3K","type":6,"title":"Sample Talk Added Apr 14 2014.....","desc":"Lorem ipsum dolor sit amet, consectetur adipiscing elit. In posuere elit odio, vel interdum ligula pretium vitae. Praesent eu orci non ipsum euismod mollis. ","c_date":"2014-04-14 18:34:44","author_name":"Sanesh Sunny","about_author":"Lorem ipsum dolor sit amet, consectetur adipiscing elit. In posuere elit odio,","authorId":"ZGNjBQN9ac3K","owner_first":"Joffin","owner":"Joffin Joy","owner_id":"ZGN0AQZ9ac3K","owner_type":"FH5R","photo":"http:\/\/localhost\/4thUser_v4\/images\/getuserphoto\/ZGN0AQZ9ac3K\/1\/small\/10443_1395738251_usr_crop.jpg","author_photo":"http:\/\/localhost\/4thUser_v4\/images\/getuserphoto\/ZGNjBQN9ac3K\/1\/small\/noimage-ind.jpg","lc_cnt":null,"ckt_name":{"ZwVk":"Trainees"}},{"id":"ZGRjAGR9ac3K","type":1,"title":"I amsdsdsdsdsd co-founder ofsdsdsds da","feed":{"is_img":0},"c_date":"2014-03-08 07:47:22","owner":"User Jolly","owner_first":"User","owner_id":"ZGNkZwH9ac3K","owner_type":"FH5R","photo":"http:\/\/localhost\/4thUser_v4\/images\/getuserphoto\/ZGNkZwH9ac3K\/1\/small\/10125_1398517242_usr_crop.JPG","lc_cnt":[{"like":"1","comment":"0"}],"ckt_name":{"ZGD3AQZ9ac3K":"Vijayee Bhava"}},{"id":"ZGRjAQx9ac3K","type":1,"title":"Lessons on Self Development\r\nhttp:\/\/localhost\/files\/vijayeebhava\/2\/Vijayeebhava_Lessons_Self_development.pptx\r\n\r\nPrepared by: Sreeja - Phoenix team","feed":{"is_img":0},"c_date":"2014-03-09 10:26:18","owner":"Vijayee Bhava","owner_first":"Vijayee Bhava","owner_id":"AQL2ZwL9ac3K","owner_type":"D09F","photo":"http:\/\/localhost\/4thUser_v4\/images\/getuserphoto\/AQL2ZwL9ac3K\/1\/small\/1394201730_46626.png","lc_cnt":[{"like":"1","comment":"0"}],"ckt_name":{"ZGD3AQH9ac3K":"Vijayee Bhava - II"}},{"id":"ZGNlAmL9ac3K","type":7,"title":"Key Lessons on LEADERSHIP","desc":"https:\/\/localhost\/files\/vijayeebhava\/2\/Vijayeebhava_Leadership_Key_Lessons.docx <\/a>","source":"","c_date":"2014-03-08 20:40:55","summary":"","owner_first":"Anaz","owner":"Anaz Kabeer","owner_id":"ZGNmZQH9ac3K","owner_type":"FH5R","photo":"http:\/\/localhost\/4thUser_v4\/images\/getuserphoto\/ZGNmZQH9ac3K\/1\/small\/10305_1382457349_usr_lr.","lc_cnt":[{"like":"1","comment":"0"}],"ckt_name":{"ZGD3AQH9ac3K":"Vijayee Bhava - II"}},{"id":"ZGNlAmp9ac3K","type":7,"title":"Qualities of an Entrepreneur","desc":"https:\/\/localhost\/files\/vijayeebhava\/2\/Vijayeebhava_Key_Lessons_Qualities_Entrepreneur.docx <\/a>","source":"","c_date":"2014-03-08 20:41:37","summary":"","owner_first":"Anaz","owner":"Anaz Kabeer","owner_id":"ZGNmZQH9ac3K","owner_type":"FH5R","photo":"http:\/\/localhost\/4thUser_v4\/images\/getuserphoto\/ZGNmZQH9ac3K\/1\/small\/10305_1382457349_usr_lr.","lc_cnt":[{"like":"1","comment":"0"}],"ckt_name":{"ZGD3AQH9ac3K":"Vijayee Bhava - II"}}],"tp_likes":"[id=\"tp_like_ZGNlAmL9ac3K_7_T\"],[id=\"tp_like_ZGNlAmp9ac3K_7_T\"],[id=\"tp_like_ZGRjAQx9ac3K_1_T\"],[id=\"tp_like_ZGRlBQR9ac3K_1_T\"]","cmnt_likes":"[id=\"tp_like_ZGRlBQR9ac3K_1_AQp1AN=9ac3K_C\"]","up_next":"2014-03-09 09:14:44","up_start":0,"up_end":"15"}
nalert('hello");". The double quote seems to invalid everything after, no ?
EDIT: Ho, as bartek said in his edit, you should try jsonlint. Thanks for the info, bartek.

Use PHP's preg_replace() to return only the value inside the <h1>?

How would I use PHP's preg_replace() to return only the value inside the <h1> in the following string (it's HTML text loaded in a variable called $html):
<h1>I'm Header</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque tincidunt porttitor magna, quis molestie augue sagittis quis.</p>
<p>Pellentesque tincidunt porttitor magna, quis molestie augue sagittis quis. Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
I've tried this: preg_replace('#<h1>([.*])</h1>.*#', '$1', $html), but to no avail. Am I regex-ing this correctly? And is there a better PHP function that I should be using instead of preg_replace?
Here is how you do it using preg_replace:
$header = preg_replace('/<h1>(.*)<\/h1>.*/iU', '$1', $html);
You can also use preg_match:
$matches = array();
preg_match('/<h1>(.*)</h1>.*/iU', $html, $matches);
print_r($matches);
([.*]) means dot OR astersk
What you need is (.*?), which means any amount of any characters ungreedy
or
([^<]*) - which means any amount of any characters but not <

php fread() strips mail newlines

If I fopen/fread a text file, then send the content in an email, the newlines in the text file do not appear in the email when it arrives.
For example, the text...
Lorem ipsum dolor sit amet,
consectetur adipiscing elit.
Sed leo erat,
rutrum posuere justo.
... arrives in the email inbox as:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed leo erat, rutrum posuere justo.
I'm using this PHP code:
$fh = fopen($email_file,'r');
$message = fread($fh,filesize($email_file));
fclose($fh);
mail("email#example.com",$subject,$message,$headers);
//Assume each variable is declared and defined.
What am I missing?
Edit: I needed to change the header Content-Type from HTML for the text version. Thanks to Yzmir and Footie. Their questions / comments led me to the correct answer.
Try replacing the new lines with <br/> and let me know if this solves it.
If you can't do it for your files, do it in your code using:
$message = nl2br($message);

Categories