473,322 Members | 1,421 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 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 2389

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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.