homepage Welcome to WebmasterWorld Guest from 54.167.144.202
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
extracting a substring from a variable
ricksum



 
Msg#: 4587934 posted 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

 

swa66

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



 
Msg#: 4587934 posted 7:56 pm on Jun 26, 2013 (gmt 0)

substr should do the trick.
[php.net...]

dmorison

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 4587934 posted 9:04 pm on Jun 26, 2013 (gmt 0)

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];

ricksum



 
Msg#: 4587934 posted 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

lucy24

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



 
Msg#: 4587934 posted 3:48 am on Jun 27, 2013 (gmt 0)

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?

ricksum



 
Msg#: 4587934 posted 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

dmorison

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 4587934 posted 4:26 pm on Jun 27, 2013 (gmt 0)

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...)

ricksum



 
Msg#: 4587934 posted 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

swa66

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



 
Msg#: 4587934 posted 7:36 am on Jun 28, 2013 (gmt 0)

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)

lucy24

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



 
Msg#: 4587934 posted 8:43 am on Jun 28, 2013 (gmt 0)

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.

Readie

WebmasterWorld Senior Member



 
Msg#: 4587934 posted 10:59 pm on Jul 2, 2013 (gmt 0)

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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved