470,833 Members | 1,389 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,833 developers. It's quick & easy.

Silly question about a strings in php

Hi,

How could I simulate the VB function Left() or Right() I would like to
return the first (x) characters of a string.

Thanks,

Kasparov
Jul 17 '05 #1
4 2320

On 13-Jan-2004, ka********@hotmail.com (Kaspa) wrote:
How could I simulate the VB function Left() or Right() I would like to
return the first (x) characters of a string.


substr()

--
Tom Thackrey
www.creative-light.com
tom (at) creative (dash) light (dot) com
do NOT send email to ja*********@willglen.net (it's reserved for spammers)
Jul 17 '05 #2
"Kaspa" <ka********@hotmail.com> wrote in message
news:e6*************************@posting.google.co m...
Hi,

How could I simulate the VB function Left() or Right() I would like to
return the first (x) characters of a string.

Thanks,

Kasparov


VB: strVar = left(strExt,12)
PHP: $strVar = substr($strExp,0,12);

VB: strVar = right(strExt,12)
PHP: $strVar = substr($strExp,-12);
or
PHP: $strVar = substr($strExp,(-1) * 12);

VB: strVar = mid(strExt,12) ' returns from chr 12 on to end of string
PHP: $strVar = substr($strExp,12);

VB: strVar = mid(strExt,12,5) ' returns from chr 12 , to 5 chrs
PHP: $strVar = substr($strExp,12,5);

Hope that helps
--
Mike Bradley
http://www.gzentools.com -- free online php tools
Jul 17 '05 #3
>How could I simulate the VB function Left() or Right() I would like to
return the first (x) characters of a string.


To make it easy on you:

<?php

/* example usage */
$string1 = "United States";
print Right($string1, 6);

/* Left function spec */
function Left($s,$n)
{

if ((empty($s)) || ($n<1)) {
return null;
}

if (strlen($s)<$n) {
$n = strlen($s);
}

return substr($s, 0, $n);

}

/* Right function spec */
function Right($s,$n)
{

if ((empty($s)) || ($n<1)) {
return null;
}

if (strlen($s)<$n) {
$n = strlen($s);
}

return substr($s, -$n);

}

?>
--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~
Wil Moore III, MCP Site : www.quicksitedesign.com?em
Application Developer Site : www.digitallysmooth.com?em
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~
Jul 17 '05 #4

"Kaspa" <ka********@hotmail.com> wrote in message
news:e6*************************@posting.google.co m...
Hi,

How could I simulate the VB function Left() or Right()
I would like to return the first (x) characters of a string.


Something like:

function Left($sourceStr, $len)
{
return substr($sourceStr, 0, $len);
}

function Right($sourceStr, $len)
{
$spos = strlen($sourceStr) - $len;
return substr($sourceStr, $spos);
}

Check out:

http://www.php.net/manual/en/ref.strings.php

for more information.

I hope this helps.

Anthony Borla
Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

15 posts views Thread by Jon Skeet | last post: by
3 posts views Thread by Nemisis | last post: by
3 posts views Thread by C# Beginner | last post: by
11 posts views Thread by Sensei | last post: by
1 post views Thread by mk | last post: by
reply views Thread by Rajanikanth Jammalamadaka | last post: by
5 posts views Thread by Ben Bacarisse | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.