Retrieving text

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Drakath13
New php-forum User
New php-forum User
Posts: 4
Joined: Sun May 11, 2014 1:24 am

Retrieving text

Postby Drakath13 » Sun May 11, 2014 1:28 am

Hello, I am new to this Forum so I hope this is the right category to post this.

$sms = $response['sms'];
This returns: username my_username
But I only want to retrieve my_username and send it to my email:
mail('myemail@gmail.com', 'Username retrieved', $sms);

How can I remove "username" from here?

User avatar
menator01
New php-forum User
New php-forum User
Posts: 6
Joined: Sat May 10, 2014 3:29 pm

Re: Retrieving text

Postby menator01 » Sun May 11, 2014 11:29 pm

You could use explode to do that. :)

Drakath13
New php-forum User
New php-forum User
Posts: 4
Joined: Sun May 11, 2014 1:24 am

Re: Retrieving text

Postby Drakath13 » Mon May 12, 2014 2:27 am

I'm not sure how does it work. What's wrong with my code?

Code: Select all

<?php
$sms = "username John";
$sms2 = explode(" ",$sms, 1);
mail('myemail@gmail.com', 'Username retrieved', $sms2);
?> 

User avatar
menator01
New php-forum User
New php-forum User
Posts: 6
Joined: Sat May 10, 2014 3:29 pm

Re: Retrieving text

Postby menator01 » Thu May 15, 2014 3:06 am

Code: Select all

<?php
$sms = 'username John';
$sms = explode(' ', $sms);
echo $sms[1];
?>


Explode will turn the string into an array

Drakath13
New php-forum User
New php-forum User
Posts: 4
Joined: Sun May 11, 2014 1:24 am

Re: Retrieving text

Postby Drakath13 » Fri May 16, 2014 3:07 am

Well, I found a nice way around with "str_replace" but I have a problem. The system often returns 3 or more texts or numbers. For example: 464 RU my_text
my_text is what I want to retrieve and it is always the last text. Is it possible to retrieve it without any other text?

User avatar
menator01
New php-forum User
New php-forum User
Posts: 6
Joined: Sat May 10, 2014 3:29 pm

Re: Retrieving text

Postby menator01 » Fri May 16, 2014 9:50 am

use explode and then array_pop

Drakath13
New php-forum User
New php-forum User
Posts: 4
Joined: Sun May 11, 2014 1:24 am

Re: Retrieving text

Postby Drakath13 » Fri May 16, 2014 11:24 am

Thanks, it worked just as I wanted.

us2rn4m2
New php-forum User
New php-forum User
Posts: 17
Joined: Thu Aug 09, 2012 1:28 pm

Re: Retrieving text

Postby us2rn4m2 » Fri May 23, 2014 1:37 pm

Code: Select all

echo strstr('username my_username', ' '); // my_username


Return to “PHP coding => General”

Who is online

Users browsing this forum: No registered users and 2 guests