472,971 Members | 1,921 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,971 software developers and data experts.

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 2371

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

81
by: Matt | last post by:
I have 2 questions: 1. strlen returns an unsigned (size_t) quantity. Why is an unsigned value more approprate than a signed value? Why is unsighned value less appropriate? 2. Would there...
15
by: Jon Skeet | last post by:
I've been briefly musing on what is probably a pretty silly idea, but one which would no doubt benefit from being discussed and thoroughly shot down in flames rather than being allowed to fester in...
3
by: Nemisis | last post by:
Hi everyone, i have currently got a classic asp web application and am wanting to upgrade it to asp.net 2.0, and also take advantage of OOP. Our current application is not using OOP so you can...
3
by: C# Beginner | last post by:
Hi there, This question might be silly to you, but anyway... How can I move a control at runtime? Object.Location.X = 48; doesn't seem to work. Do I really need the code "Object.Location =...
11
by: Sensei | last post by:
Hi again! I have ``yet another silly question'', about arrays this time. I've looked through the FAQs in the array & memory sections without finding an answer. I surely didn't look deep enough. ...
1
by: mk | last post by:
Out of curiosity I decided to make some speed comparisons of the same algorithm in Python and C++. Moving slices of lists of strings around seemed like a good test case. Python code: def...
0
by: Rajanikanth Jammalamadaka | last post by:
Try using a list instead of a vector for the C++ version. Raj On Tue, Jul 8, 2008 at 3:06 PM, mk <mrkafk@gmail.comwrote: -- "For him who has conquered the mind, the mind is the best of...
5
by: Ben Bacarisse | last post by:
Newbie <newbie@gmail.comwrites: <snip> The version you posted in comp.programming has the correct loop. Why change it? -- Ben.
10
by: r035198x | last post by:
There are a lot of common C mistakes. I have put together the ones I consider to be the silliest made by C programmers. These are usually the source of the popular C programming tears and remembering...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.