Welcome to WebmasterWorld Guest from 54.158.36.59

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

extracting a substring from a variable

     
7:42 pm on Jun 26, 2013 (gmt 0)



Problem, I'm trying to extract from a php variable a substring that begins at a specific location for a length.

For example:

$mybook="Last Night";

What I want to do is extract Last using a variable for the starting position and a variable for the ending location.

Can someone help me
7:56 pm on Jun 26, 2013 (gmt 0)

WebmasterWorld Senior Member swa66 is a WebmasterWorld Top Contributor of All Time 10+ Year Member



substr should do the trick.
[php.net...]
9:04 pm on Jun 26, 2013 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi ricksum,

As swa66 says, substr sounds like just what you want, but the explode() function is handy for string processing if you're after a specific word (e.g. the first word)

$mybook="Last Night";
$words = explode(" ",$mybook);
print $words[0]; // "Last"
print $words[1]; // "Night"

Or to programatically get the last word in a string;

print $words[count($words)-1];
1:19 am on Jun 27, 2013 (gmt 0)



I tried the substr but didn't work. Here's my code:
$last=substr($mybook,$firstspace,$lengthofstring);
also tried $last=substr($mybook,'$firstspace','$lengthofstring');
What am I doing wrong?
Thanks
3:48 am on Jun 27, 2013 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



For example:

$mybook="Last Night";

What I want to do is extract Last using a variable for the starting position and a variable for the ending location.

You're saying two different things here. They're not necessarily contradictory or mutually exclusive, but you need to be clear what the two different things are.

#1 "I want to extract the first word in a multi-word string, where the words are delimited by spaces" (leaving non-word, non-space characters out of the mix)

#2 "I want to extract a substring beginning at a specified numerical location and ending at a different specified location" (or "ending after a specified number of characters" -- so, OK, two and a half things).

So the question is
#1 do you want to feed in some numbers that, in the example given, will happen to return the string "Last"
or
#2 do you want to perform a function that, in the example given, will happen to return the first four characters of the original string?
11:25 am on Jun 27, 2013 (gmt 0)



Sorry I wasn't very clear but #2 is what I want. Let me try to be a little clearer, I hope.

I have a string that contains a username which consists of a last name, first name (separated by a space and if the user has a mate that name will be separated by an &.
I know how to find the location of all the spaces (let's say the username is Hahn Jerry Don & Julia Fay). What I need are variables that contain Hahn as last, Jerry Don as Husband and Julia Fay as Mate.

I would prefer to use mysql to accomplish this but php would do.

Thanks
4:26 pm on Jun 27, 2013 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



With

$mystring = "Hahn Jerry Don & Julia Fay";

Split out the username and mate first of all using explode() on "&"

$parts = explode("&",$mystring);
$username = trim($parts[0]);
$mate = trim($parts[1]);

You now have $mate as either an empty string ("") or the full text of the mate, less any supurflous leading or trailing spaces.

Finally, it sounds like you want to split $username into the first word (last name), and all remaining words (first name) so that would be

$s = strpos($username," ");
$lastname = substr($username,0,$s);
$firstname = substr($username,($s+1));

...putting it all together:


<?php
$mystring = "Hahn Jerry Don & Julia Fay";
$parts = explode("&",$mystring);
$username = trim($parts[0]);
$mate = trim($parts[1]);
$s = strpos($username," ");
$lastname = substr($username,0,$s);
$firstname = substr($username,($s+1));
print "<p>Last Name: ".$lastname."</p>";
print "<p>First Name(s): ".$firstname."</p>";
if ($mate)
{
print "<p>Mate: ".$mate."</p>";
}
else
{
print "<p>".$username." has no mate!</p>";
}
?>


HTH!

(meta: doesn't WebmasterWorld have a way to post code that preserves white space formatting? I'm sure it used to have...)
6:23 pm on Jun 27, 2013 (gmt 0)



dmorison,

Thank you so much. I've been searching the web for at least a week and have tried most of the functions you listed I just wasn't putting them together in the correct sequence. I'm very new to php so am having a hard time learning all the particulars. Thanks again
7:36 am on Jun 28, 2013 (gmt 0)

WebmasterWorld Senior Member swa66 is a WebmasterWorld Top Contributor of All Time 10+ Year Member



OT:
WebmasterWorld have a way to post code that preserves white space formatting?

Convoluted:


[quote][pre][code]
will preserve spaces (well kind of: it will half the number of spaces on each edit and each preview
[/code][/pre][/quote]
also take care with too long lines a you have to wrap them yourself (or a mod will do it causing yet again a halving of your spaces)
8:43 am on Jun 28, 2013 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



If you make every other one a nonbreaking space, they'll be preserved-- at least until the moment the thread is read by someone using a different file encoding. They'll see a string of (A-circumflex) instead and will wonder what the ### you were getting at :)

Whoops! Different thread.
10:59 pm on Jul 2, 2013 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



There's a much less wordy means of doing this using a regular expression.

Also, in the event of there not being a mate, this code won't throw a notice [php.net], while the above will with this line:
$mate = trim($parts[1]);


$str = 'Hahn Jerry Don & Julia Fay';
preg_match('/^([^ ]+) (.*?)(?: & (.*))?$/', $str, $matches);

echo "Last name: {$matches[1]} - Husband: {$matches[2]}";
if(isset($matches[3])) {
echo " - Mate: {$matches[3]}";
}


EDIT:
If you haven't learnt regex yet, I recommend taking a look here [regular-expressions.info] and here [regular-expressions.info]. It's a fantastically useful tool and well worth learning.
 

Featured Threads

Hot Threads This Week

Hot Threads This Month