I am trying to grab only usernames in a list of sentences of about 30 lines long. I am able to get it to grab the usernames and everything afterwards but I dont want to get everything after the username.
test.txt
[India] Hagiwara has been inactive for 33 days (last seen: Sun Jan 25 23:35:35 2015).
[India] Psyborg has been inactive for 35 days (last seen: Fri Jan 23 18:43:58 2015).
[Echo] Pela has been inactive for 31 days (last seen: Tue Jan 27 20:00:30 2015).
PHP
$data = file('test.txt');
foreach ($data as $lines) {
if (($pos = strpos($lines, "]")) !== FALSE) {
$string = substr($lines, $pos+1);
}
echo $string . '<br />';
}
Output
Hagiwara has been inactive for 33 days (last seen: Sun Jan 25 23:35:35 2015).
This may be more than you expected, but here is a way to avoid regex.
$string = substr($lines, $pos+2);
$string = substr($string, 0, strpos($string, ' '));
$username = explode(' ',$lines)[1];
Given your example strings and based on the assumption that a username will never contain whitespace characters:
<?php
$input_line = "[India] Hagiwara has been inactive for 33 days (last seen: Sun Jan 25 23:35:35 2015).";
if (preg_match("/\[.+\]\s*(\S+)\s*.*/", $input_line, $output_array) == 1) {
echo $output_array[1];
}
?>
Will print out "Hagiwara".
The Regex to do this isn't so complicated. It's more bulletproof imho
$str1 = '[India] Hagiwara has been inactive for 33 days (last seen: Sun Jan 25 23:35:35 2015).';
$str2 = '[India] Psyborg has been inactive for 35 days (last seen: Fri Jan 23 18:43:58 2015).';
$str3 = '[Echo] Pela has been inactive for 31 days (last seen: Tue Jan 27 20:00:30 2015).';
$strs = array($str1,$str2,$str3);
foreach ($strs as $key => $val) {
//strips the initial bracketed string from the beginning of the line
$trimmed_val = preg_replace("/^\[\w+\]\s/",'',$val);
//grabs the first word from the trimmed string (user in this case)
preg_match("/^\w+\s/",$trimmed_val,$users);
//prints out a list of users for demonstration
echo $users[0] . '<br/>';
}
I have the following JSON from a Mailgun webhook (Delivered) that I need to extract the subject line from.
They do not appear to be following a typical key value JSON format, short of a bunch of foreach loops is there a way to extract this data?
[["Received", "by luna.mailgun.net with SMTP mgrt 8734663311733; Fri, 03 May 2013 18:26:27 +0000"], ["Content-Type", ["multipart/alternative", {"boundary": "eb663d73ae0a4d6c9153cc0aec8b7520"}]], ["Mime-Version", "1.0"], ["Subject", "Test deliver webhook"], ["From", "Bob <bob#fvrs.org>"], ["To", "Alice <alice#example.com>"], ["Message-Id", "<20130503182626.18666.16540#fvrs.org>"], ["X-Mailgun-Variables", "{\"my_var_1\": \"Mailgun Variable #1\", \"my-var-2\": \"awesome\"}"], ["Date", "Fri, 03 May 2013 18:26:27 +0000"], ["Sender", "bob#fvrs.org"]]
The reason why it's not a dictionary is because in emails you can have the same header appear more than once.
You just a single loop, though:
$subject = null;
foreach ($data as $header) {
if ($header[0] == 'Subject') {
$subject = $header[1];
break;
}
}
I need away to tell if a $_POST[phone] is starting with 04 if it is I want run a query
In javascript I know how to do this but for some reason on Ubuntu 8 (Yes I know its old) and Firefox the agents are able to still post 04 number in mobile field after testing my javascript query over and over again - I thought instead
You can do a substr like this:
if (substr($_POST['phone'], 0, 2) == '04') {
// ... do some code here
}
If you want to match multiple strings at the same time you could do this (it's faster than doing an in_array()):
$prefixes = array('02'=>true, '03'=>true, '07'=>true, '08'=>true);
$inputPrefix = substr($_POST['phone'], 0, 2);
if (isset($prefixes[$inputPrefix])) {
// ... do some code here
}
Based on your comment if you want to make sure it is only 03, 02, 07 or 08 you can do the following:
$telephonePrefix = substr($_POST['phone'], 0, 2);
$knowPrefixes = array('03','02','07','08');
if (in_array($telephonePrefix , $knowPrefixes)) {
// ... do some code here
}
I am trying to get the events for my fullCalendar implementation with the "events (as a JSON feed)" built in feature.
...
event: 'calendarFill.php',
...
The php file returns the following JSON data:
[{"id":"40","title":"test four","services":"Reflexology (40), foot-soak","start":"Tue Mar 26 2013 13:00:00","end":"Tue Mar 26 2013 13:40:00","color":"#e56b15","customerId":"21","phone":"555-0404","email":"test4","notes":"test two of update. this sentence added at update.","seat":"Side Couch 9","vip":"yes","confirmed":"yes","knows_policies":"yes","customer_here":"0","allDay":"false"},{"id":"41","title":"test four","services":"Foot-Soak, ","start":"Wed Mar 27 2013 19:00:00","end":"Wed Mar 27 2013 19:15:00","color":"#e56b15","customerId":"21","phone":"555-0404","email":"test4","notes":"test of addslashes. what's going to happen?","seat":"Front Chair 2","vip":"yes","confirmed":"yes","knows_policies":"yes","customer_here":"0","allDay":"false"}]
The above does not work - the events are not displayed on the calendar. I've cut and pasted the code directory in my Javascript code:
events: {"id":"40","title":"test four","services":"Reflexology (40), foot-soak","start":"Tue Mar 26 2013 13:00:00","end":"Tue Mar 26 2013 13:40:00","color":"#e56b15","customerId":"21","phone":"555-0404","email":"test4","notes":"test two of update. this sentence added at update.","seat":"Side Couch 9","vip":"yes","confirmed":"yes","knows_policies":"yes","customer_here":"0","allDay":"false"},{"id":"41","title":"test four","services":"Foot-Soak, ","start":"Wed Mar 27 2013 19:00:00","end":"Wed Mar 27 2013 19:15:00","color":"#e56b15","customerId":"21","phone":"555-0404","email":"test4","notes":"test of addslashes. what's going to happen?","seat":"Front Chair 2","vip":"yes","confirmed":"yes","knows_policies":"yes","customer_here":"0","allDay":"false"}],
And, it does not display. But, if I edit the above to remove all the quotes that are not surrounding text values (as below), IT WORKS.
events: [{id:40,title:"test four",services:"Reflexology (40), foot-soak",start:"Tue Mar 26 2013 13:00:00",end:"Tue Mar 26 2013 13:40:00",color:"#e56b15",customerId:21,phone:"555-0404",email:"test4",notes:"test two of update. this sentence added at update.",seat:"Side Couch 9",vip:"yes",confirmed:"yes",knows_policies:"yes",customer_here:0,allDay:false},{id:41,title:"test four",services:"Foot-Soak, ",start:"Wed Mar 27 2013 19:00:00",end:"Wed Mar 27 2013 19:15:00",color:"#e56b15",customerId:21,phone:"555-0404",email:"test4",notes:"test of addslashes. what's going to happen?",seat:"Front Chair 2",vip:"yes",confirmed:"yes",knows_policies:"yes",customer_here:0,allDay:false}],
I am using the documentation at this link: http://arshaw.com/fullcalendar/docs/event_data/events_json_feed/
What am I doing wrong? Should I remove the 'json_encode' from my php and just return a string formatted like the above? I'd like to do it the 'right' way and not some work-around.
Added the php as requested:
// for connection to db
include("../includes/config.php");
include("../includes/mysql_functions.php");
// connection with db
$linkID = db_connect();
$returnValue = array();
$getEventData = mysql_query("SELECT apt_num, services, apt_date_start, apt_date_end, therapist, customer_num, notes, seat, confirmed, knows_policies, here FROM appointments", $linkID);
if ($getEventData != FALSE && mysql_num_rows($getEventData) > 0)
{
while ($theEventData = mysql_fetch_array($getEventData))
{
$getCustomerString = "SELECT first_name, middle_name, last_name, phone, email, vip FROM customer WHERE customer_num = ".$theEventData['customer_num'];
$getCustomerData = mysql_query($getCustomerString, $linkID);
if ($getCustomerData != FALSE && mysql_num_rows($getCustomerData) > 0)
{
while($theCustomerData = mysql_fetch_array($getCustomerData))
{
$customerName = $theCustomerData['first_name']." ".$theCustomerData['middle_name']." ".$theCustomerData['last_name'];
$customerPhone = $theCustomerData['phone'];
$customerEmail = $theCustomerData['email'];
$customerVip = $theCustomerData['vip'];
}
}
else
{
$customerName = "error";
$customerPhone = "error";
$customerEmail = "error";
$customerVip = "error";
}
$rowArray['id'] = $theEventData['apt_num'];
$rowArray['title'] = $customerName;
$rowArray['services'] = $theEventData['services'];
$rowArray['start'] = date("D", $theEventData['apt_date_start'])." ".date("M", $theEventData['apt_date_start'])." ".date("d", $theEventData['apt_date_start'])." ".date("Y", $theEventData['apt_date_start'])." ".date("H", $theEventData['apt_date_start']).":".date("i", $theEventData['apt_date_start']).":".date("s", $theEventData['apt_date_start']);
$rowArray['end'] = date("D", $theEventData['apt_date_end'])." ".date("M", $theEventData['apt_date_end'])." ".date("d", $theEventData['apt_date_end'])." ".date("Y", $theEventData['apt_date_end'])." ".date("H", $theEventData['apt_date_end']).":".date("i", $theEventData['apt_date_end']).":".date("s", $theEventData['apt_date_end']);
$rowArray['color'] = $theEventData['therapist'];
$rowArray['customerId'] = $theEventData['customer_num'];
$rowArray['phone'] = $customerPhone;
$rowArray['email'] = $customerEmail;
$rowArray['notes'] = $theEventData['notes'];
$rowArray['seat'] = $theEventData['seat'];
$rowArray['vip'] = $customerVip;
$rowArray['confirmed'] = $theEventData['confirmed'];
$rowArray['knows_policies'] = $theEventData['knows_policies'];
$rowArray['customer_here'] = $theEventData['here'];
$rowArray['allDay'] = "false";
array_push($returnValue, $rowArray);
}
}
else
{
$returnValue[0] = "error";
}
print json_encode($returnValue);
Markus Vetter (from Google+) found the problem. In the original code returned from my php file there is an "'" that causes an issue. I needed to 'addslashes' to my php before returning it to the jQuery/Javascript. Once that was done, the jQuery / fullCalendar code was able to render my events as expected.
A second pair of eyes does wonders! Thank you Markus Vetter!
The original code returned:
[{"id":"40","title":"test four","services":"Reflexology (40), foot-soak","start":"Tue Mar 26 2013 13:00:00","end":"Tue Mar 26 2013 13:40:00","color":"#e56b15","customerId":"21","phone":"555-0404","email":"test4","notes":"test two of update. this sentence added at update.","seat":"Side Couch 9","vip":"yes","confirmed":"yes","knows_policies":"yes","customer_here":"0","allDay":"false"},{"id":"41","title":"test four","services":"Foot-Soak, ","start":"Wed Mar 27 2013 19:00:00","end":"Wed Mar 27 2013 19:15:00","color":"#e56b15","customerId":"21","phone":"555-0404","email":"test4","notes":"test of addslashes. what's going to happen?","seat":"Front Chair 2","vip":"yes","confirmed":"yes","knows_policies":"yes","customer_here":"0","allDay":"false"}]
After using 'addslashes' in my php for all text fields:
[{"id":"40","title":"test four","services":"Reflexology (40), foot-soak","start":"Tue Mar 26 2013 13:00:00","end":"Tue Mar 26 2013 13:40:00","color":"#e56b15","customerId":"21","phone":"555-0404","email":"test4","notes":"test two of update. this sentence added at update.","seat":"Side Couch 9","vip":"yes","confirmed":"yes","knows_policies":"yes","customer_here":"0","allDay":"false"},{"id":"41","title":"test four","services":"Foot-Soak, ","start":"Wed Mar 27 2013 19:00:00","end":"Wed Mar 27 2013 19:15:00","color":"#e56b15","customerId":"21","phone":"555-0404","email":"test4","notes":"test of addslashes. what\'s going to happen?","seat":"Front Chair 2","vip":"yes","confirmed":"yes","knows_policies":"yes","customer_here":"0","allDay":"false"}]
Actually solved this time! In my php file, the last value added to the $rowArray is: $rowArray['allDay'] = "false". Instead of being a text value, it needs to be the actual boolean value. This "$rowArray['allDay'] = false" fixed the problem.
I had the same issue and it was a formatting thing for me as well.
This is how my json is returning now and it is finally working.
[{"title":"Coke Trade Show","start":"2014-12-03"},{"title":"Michigan Show","start":"2014-12-04"}]
Definitely a good idea to paste your json return straight into the JS to make sure it is working.
Particularly, 08 and 09 have caused me some major trouble. Is this a PHP bug?
Explanation:
I have a calendar 'widget' on a couple of our client's sites, where we have a HTML hard-coded calendar (I know a PHP function can generate n number of months, but the boss man said 'no').
Within each day, there is a PHP function to check for events on that day, passing the current day of the month like so:
<td valign="top">01<?php printShowLink(01, $events) ?></td>
$events is an array of all events on that month, and the function checks if an event is on that day:
function printShowLink($dayOfMonth, $eventsArray) {
$show = array();
$printedEvent = array();
$daysWithEvents = array();
foreach($eventsArray as $event) {
if($dayOfMonth == $event['day'] && !in_array($event['id'], $printedEvent)){
if(in_array($event['day'], $daysWithEvents)) {
echo '<hr class="calendarLine" />';
} else {
echo '<br />';
}
$daysWithEvents[] = $event['day']; // string parsed from timestamp
if($event['linked'] != 1) {
echo '<div class="center cal_event '.$event['class'].'" id="center"><span title="'.$event['title'].'" style="color:#666666;">'.$event['shorttitle'].'</span></div>';
$printedEvent[] = $event['id'];
} else {
echo '<div class="center cal_event '.$event['class'].'" id="center">'.$event['shorttitle'].'</div>';
$printedEvent[] = $event['id'];
}
}
}
}
On the 8th and 9th, no events will show up. Passing a string of the day instead of a zero-padded integer causes the same problem.
The solution is as what is should have been in the first place, a non-padded integer. However, my question is, have you seen this odd behavior with 08 and/or 09?
I googled this and couldn't find anything out there.
Quote it. 0123 without quotes is octal in PHP. It's in the docs
$ php -r 'echo 01234, "\n01234\n";'
668
01234
$
So you should change your code to
<td valign="top">01<?php printShowLink('01', $events) ?></td>
It's been a while since I've had to wade through so much PHP been doing mostly Javascript for 3 years. But 08 and 09 being a problem makes me think: they could be getting treated as octal (base 8), and the digits 8 and 9 do not exist in octal.