How do I read / get contents of a text message body? - php

I'm going to use a SMS to Web Server, which will forward incoming text messages to my web server.
The message body will contain some short codes, a mobile number and a amount in a format. I need to store the details separately to variables.
Here's an example, what I will receive in my web server through POST method:
$sender = "9999912345";
$date = "2017-04-14 13:04:40";
$message = "RCG ARTL 9700012345 50";
I need to store the contents of $message separately to variables:
$cmd = "RCG";
$op = "ARTL";
$no = "9700012345";
$amt = "50";
Please help!

Please try this code:
$message = "RCG ARTL 9700012345 50";
$arr = explode(" ", $message);
$cmd = $arr[0];
$op = $arr[1];
$no = $arr[2];
$amt = $arr[3];

<?php
$message = "RCG ARTL 9700012345 50";
list($cmd, $op, $no, $amt) = explode(" ",$message);
you can test it.

Try this code, use explode function with space as the delimiter and then make sure exploded message (an array) is contains 4 key/value.
$cmd = '';
$op = '';
$no = '';
$amt = '';
$explodedMessage = explode(' ', $message);
if (count($explodedMessage)==4){
$cmd.=$explodedMessage[0];
$op.=$explodedMessage[1];
$no.=$explodedMessage[2];
$amt.=$explodedMessage[3];
}

You can try this.
$message = "RCG ARTL 9700012345 50";
list($cmd, $op, $no, $amount) = explode(' ', $message);
var_dump($cmd, $op, $no, $amount);
Output:
string(3) "RCG"
string(4) "ARTL"
string(10) "9700012345"
string(2) "50"

You can convert the $message into an array using explode function where the delimitre will be a space. and then can access it the way you want it.
$message = "RCG ARTL 9700012345 50";
$messageArray = explode(" ", $message);
$someText = $messageArray[0];

Related

Inserting PHP objects to database

<?php
class Prof {
function get_block($lines, $prof_num) {
$reserve = 200;
$strings = implode("", $lines);
$start_line = preg_grep("/1\.$prof_num\.1\./m", $lines);
$prof_num+=1;
$end_line = preg_grep("/1\.$prof_num\.1\./m", $lines);
$prof_num-=1;
$start_pos = mb_strpos($strings, end($start_line));
$end_pos = mb_strpos($strings, end($end_line));
$finalContent = mb_substr($strings, $start_pos-$reserve, $end_pos-$start_pos+$reserve);
$from = "1.$prof_num";
$prof_num+=1;
$to = "1.$prof_num";
$cutted = mb_substr($finalContent, strpos($finalContent, $from), strpos($finalContent, $to));
$cutted = preg_replace("/$to\..*/", "", $cutted);
// echo $cutted;
// $this->get_id($cutted);
// $this->get_name($cutted);
}
function get_id($txt) { //gettind id
$txt = explode("\n", $txt);
$id = preg_grep("/\d\.\sCode\s/", $txt);
$id = preg_replace("/\d\.\sCode\s\–\s/","", $id);
$id = preg_replace("/\t/", "",$id);
$id = preg_replace("/\.\s+/", "",$id);
foreach($id as $item) {
echo $item ."\n";
}
}
function get_name($txt) { //getting name
$txt = explode("\n", $txt);
$name = preg_grep("/\sName/", $txt);
$name = preg_replace("/\d\.\sName\–\s/","",$name);
$name = preg_replace("/\t/", "",$name);
$name = preg_replace("/\.\s+/", "",$name);
foreach($name as $item) {
echo $item ."\n";
}
}
Obj1 = new Prof();
Obj1 -> get_name(get_block(file('text.txt'))); //ERROR HERE!!!!!!!!
How can I get to get_id and get_name through get_block method?
- get_block gets me the whole text from text file, starting from line number $prof_num.
- get_id gets me the id from the get_block text block. get_name just gets the name from
get_block.
I need to write these methods (id and name) to my database, but as I know, I can't do it, if I only manage to get text block with get_block.
Pls, ask me if something, maybe I just can't explain it. :D
This is wrong:
Obj1 -> get_name(get_block(file('text.txt')))
The function get_block() is not defined, it is a method of your Pro class.
So you would need:
$Obj1->get_name($Obj1->get_block(file('text.txt')));
or, if it is a static method:
$Obj1->get_name(Pro::get_block(file('text.txt')));
Also note that get_block() is defined as a method that takes 2 parameters, something that looks like a string (based on the name...) and a number (based on the name...). So sending it only an array - the return value of file() - will also fail.

Using Preg_replace with Json

So I have attempted to get anything with a #texthere t change to a link so i can navigate them to the hash page of that specific hash.
With the code I have I just keep getting 'undefined' back. Can someone please take a look and point out where I am going wrong.
$json = array(
'userpost' => array()
);
$row = mysqli_fetch_assoc($check1);
$posts = array();
$posts['num'] = $num;
$posts['streamitem_id'] = $row['streamitem_id'];
$autoembed = new AutoEmbed();
$posts['streamitem_content'] = $autoembed->parse($row['streamitem_content']);
$regex = "/#(\w+)/";
$string=$row['streamitem_content'];
$string = preg_replace($regex, '$1', $string);
$posts['streamitem_content']=json_decode($string);
$posts['streamitem_creator'] = $row['streamitem_creator'];
$posts['streamitem_timestamp'] = $row['streamitem_timestamp'];
$posts['username'] = $row['username'];
$posts['id'] = $row['id'];
$posts['first'] = $row['first'];
$posts['middle'] = $row['middle'];
$posts['last'] = $row['last'];
$json['userpost'][] = $posts;
echo json_encode($json);
Okay this is what I've done to fix the issue. no decoding needed and that is what was causing the issue.
$regex = "/#(\w+)/";
$posts['streamitem_content'] = $row['streamitem_content'];
$posts['streamitem_content'] = preg_replace($regex, "
<a href='hash.php?tag=$1'>$1</a>", $posts['streamitem_content'] );

preg_replace inside of PHP file

I have a file that i'm parsing And I'm trying to replace $mail["email_from"] = "test#example.com"; with $mail["email_from"] = request("email");,(meaning that I want to replace all the lines that has $mail["email_from"] at the begining an ; at the end) and here's my preg_replace:
$email = "$mail[\"email_from\"] = request(\"email\")";
$newcontent = preg_replace("/\$mail[\"email_from\"](.+);/",$email,$content);
What's the error in my code? and how to fix it? Much appreciated
DEMO
After using good quotes and escaping all chars you need, this works:
$email = '$mail["email_from"] = "test#example.com";';
$replacement = '$mail["email_from"] = request("email");';
$newContent = preg_replace('/\\$mail\\[\\"email_from\\"\\](.+);/i', $replacement, $email);
echo $newContent; //$mail["email_from"] = request("email");
$email = "$mail[\"email_from\"] = request(\"email\")";
^---double-quoted string
^^^^^---array reference
You probably need
$email = "\$mail[\"email_from\"] = request(\"email\")";
^--escape the $
Use ^ and $ to specify beginning and end of line. Special characters like $, [, and ] need to be escaped.
<?php
$content = '$mail["email_from"] = "test#example.com";';
$email = '$mail["email_from"] = request("email");';
$newcontent = preg_replace('/^\$mail\["email_from"\] =.+;$/',$email,$content);
echo $newcontent . "\n";
outputs:
$mail["email_from"] = request("email");

scrape email addresses

fff.html is an email with email addresses in it some have href mailto links and some don't, i want to scrape them and output them into the following format
Lorem#ipsum.com,dolor#sit.com,amet#consectetur.com
I have a simple scraper to get the ones that are href linked but something is wierd
<?php
$url = "fff.html";
$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,'<a href="mailto:');
$end = strpos($content,'"',$start) + 8;
$mail = substr($content,$start,$end-$start);
print "$mail<br />";
?>
I should get extra points for the original use of lorem ipsum
The problem is what if you have more than one email address in the HTML page. substr will only return the first instance. Here is a script that will parse all email addresses. You may need to tweak it some for your use. It will output the results in the CSV form you requested.
<?php
$url = "fff.html";
$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, '<body>');
$end = strpos($content, '</body>');
$data = substr($content, $start, $end-$start);
$pattern = '#a[^>]+href="mailto:([^"]+)"[^>]*?>#is';
preg_match_all($pattern, $data, $matches);
foreach ($matches[1] as $key => $email) {
$emails[] = $email;
}
echo implode(', ', $emails );
?>

How can I break an email address into different parts in php?

Basically what I want to do is display an email using javascript to bring the parts together and form a complete email address that cannot be visible by email harvesters.
I would like to take an email address eg info#thiscompany.com and break it to:
$variable1 = "info";
$variable2 = "thiscompany.com";
All this done in PHP.
Regards,
JB
list($variable1, $variable2) = explode('#','info#thiscompany.com');
$parts = explode("#", $email_address);
Assuming that $email_address = 'info#thiscompany.com' then $parts[0] == 'info' and $parts[1] == 'thiscompany.com'
You can use explode:
$email = 'info#thiscompany.com';
$arr = explode('#',$email);
$part1 = $arr[0]; // info
$part2 = $arr[1]; // thiscompany.com
$email = "info#thiscompany.com";
$parts = explode("#", $email);
Try this one before you roll your own (it does a lot more):
function hide_email($email)
{ $character_set = '+-.0123456789#ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz';
$key = str_shuffle($character_set); $cipher_text = ''; $id = 'e'.rand(1,999999999);
for ($i=0;$i<strlen($email);$i+=1) $cipher_text.= $key[strpos($character_set,$email[$i])];
$script = 'var a="'.$key.'";var b=a.split("").sort().join("");var c="'.$cipher_text.'";var d="";';
$script.= 'for(var e=0;e<c.length;e++)d+=b.charAt(a.indexOf(c.charAt(e)));';
$script.= 'document.getElementById("'.$id.'").innerHTML=""+d+""';
$script = "eval(\"".str_replace(array("\\",'"'),array("\\\\",'\"'), $script)."\")";
$script = '<script type="text/javascript">/*<![CDATA[*/'.$script.'/*]]>*/</script>';
return '<span id="'.$id.'">[javascript protected email address]</span>'.$script;
}
How about a function for parsing strings according to a given format: sscanf. For example:
sscanf('info#thiscompany.com', '%[^#]#%s', $variable1, $variable2);

Categories