473,394 Members | 1,715 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,394 software developers and data experts.

substr/ereg question

Hi Group,

I want to reprint a part of a form if the length of the input is higher than
a predetermed length.
For instance, if someone posts a name, longer than 15 characters, I want to
cut it back to 15 characters and ask for confirmation on a control sheet.

I already slapped myself for doing this:

if($name=substr($name,0,15)){//take action//}

I thought this would only be true is the string itself was actually cut...
big bullpoop it is of course :(
Now... I think it's pretty much clear what I want.

a: cut string IF longer than 15 chars.
b: set flag IF string's been cut.

Is there someone who knows a regexp maybe that does this and who's willing
to share and save me some time?

Thanks,

Michel
Jul 17 '05 #1
9 1421
MIchel wrote:
I already slapped myself for doing this:

if($name=substr($name,0,15)){//take action//}

I thought this would only be true is the string itself was actually cut...
big bullpoop it is of course :(
Now... I think it's pretty much clear what I want.
When assigning a value to a variable, will always be true, so what you are
doing is cutting down the string to mac 15 characters.
a: cut string IF longer than 15 chars.
if( strlen($name)>15 ) {
b: set flag IF string's been cut.


$shortname=substr($name,0,15);

}

you can test the flag with

if(isset($shortname) {
/* do what ever you want */
}
//Aho
Jul 17 '05 #2
if( strlen($name)>15 ) {
b: set flag IF string's been cut.


$shortname=substr($name,0,15);

}

you can test the flag with

if(isset($shortname) {
/* do what ever you want */
}


Hi J.O.,

Yeah. I can do it like that, but it's not quite what I had in mind... there
has to be a way to do this in a oneliner. something like:

$result=(cutthisstringiflongerthan15) that both does the cutting and sets
$result to TRUE.

I was thinking some regexp.

No?
Jul 17 '05 #3
MIchel wrote:
if( strlen($name)>15 ) {

b: set flag IF string's been cut.


$shortname=substr($name,0,15);

}

you can test the flag with

if(isset($shortname) {
/* do what ever you want */
}

Hi J.O.,

Yeah. I can do it like that, but it's not quite what I had in mind... there
has to be a way to do this in a oneliner. something like:

$result=(cutthisstringiflongerthan15) that both does the cutting and sets
$result to TRUE.

I was thinking some regexp.

No?


don't think so, I fear you would get a true in each case.
//Aho
Jul 17 '05 #4
MIchel wrote:
Yeah. I can do it like that, but it's not quite what I had in mind...
there has to be a way to do this in a oneliner. something like:

$result=(cutthisstringiflongerthan15) that both does the cutting and
sets $result to TRUE.


For this, you will need to write a function that takes a string as an
argument by reference, cuts it and returns the remaining piece or nothing
when the string isn't longer than the defined length, which equals to false.

Example:

function cut_string(&$string, $length) {
if (preg_match("/^(.{".$length."})(.*)$/", $string, $matches)) {
list(,$string,$result) = $matches;
return $result;
}
}

$mystring = "astringthatismuchlongerthan15chars";
if ($result = cut_string($mystring, 15)) {
// $mystring exceeds 15 characters in length
}
JW

Jul 17 '05 #5
Janwillem Borleffs wrote:
For this, you will need to write a function that takes a string as an
argument by reference, cuts it and returns the remaining piece or
nothing when the string isn't longer than the defined length, which
equals to false.


Nah, can be done without the custom function:

$mystring = "astringthatismuchlongerthan15chars";
list($mystring, $result) = preg_split("/(?<=.{15})/", $mystring);

if ($result) {
// $mystring exceeds 15 characters in length
}
JW

Jul 17 '05 #6
Janwillem Borleffs wrote:
list($mystring, $result) = preg_split("/(?<=.{15})/", $mystring);


The above will throw a warning when $mystring contains less then 15
characters. This can be fixed as follows:

list($mystring, $result) = preg_split("/(?<=.{15})|$/", $mystring);
JW

Jul 17 '05 #7
Thank you all very much for your suggestions. I have a working solution now.

Michel

"Janwillem Borleffs" <jw@jwscripts.com> wrote in message
news:42***********************@news.euronet.nl...
Janwillem Borleffs wrote:
list($mystring, $result) = preg_split("/(?<=.{15})/", $mystring);


The above will throw a warning when $mystring contains less then 15
characters. This can be fixed as follows:

list($mystring, $result) = preg_split("/(?<=.{15})|$/", $mystring);
JW

Jul 17 '05 #8
"MIchel" <no@spam.please> writes:
Yeah. I can do it like that, but it's not quite what I had in mind... there
has to be a way to do this in a oneliner. something like:


Right. There HAS to be a way and someone did eventually post one or
more examples.

Why so hell bent on a one liner? I think you will learn from
experience that these one-liners are often terse and unreadable and
merit at least a one-liner comment along with them (we are speaking
about mature, production quality code now).

if(strlen)$input_string) > 15)
{
cut string;
do something special
}

Easy to write, easy to read, self-documenting.

But you wanted a one-liner. Well, here's a tru one-liner using no
regexp and with the test also on the same line.

$string = 'well, hello there !!!';
$length = 15;

if($string !== ($string = substr($string, 0, $length)))
print $string;

YMMV

--
-------------------------------------------------------------------------------
Jerry Sievers 305 854-3001 (home) WWW ECommerce Consultant
305 321-1144 (mobile http://www.JerrySievers.com/
Jul 17 '05 #9
MIchel wrote:

a: cut string IF longer than 15 chars.
b: set flag IF string's been cut.

What about:

$shortname = strlen($name)>15?substr($name,0,15):false;

grt,
Evert
www.rooftopsolutions.nl
Jul 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: bonehead | last post by:
Greetings, I'd like to add somee error handling to a password field: if the password is fewer than 5 characters or greater than 15 characters I'd like to trap it. Special characters such as ^...
1
by: Stefan Gangefors | last post by:
I'm trying to figure out what I'm doing wrong when using ereg(). This is my regexp: ereg("^]$", "]"); and that does'n work, but this does: ereg("^$", "[");
9
by: Fiore Alessandro | last post by:
Hi all I've installed Apache 2.0.48 and PHP 4.3.4 on a workstation running Linux, RedHat 9.0. Let's consider the following piece of code: $patt = "{3}/{6}"; if...
3
by: Martin Lucas-Smith | last post by:
Is there some way of using ereg to detect when certain filename extensions are supplied and to return false if so, WITHOUT using the ! operator before ereg () ? I have an API that allows as an...
3
by: Rafal Zak | last post by:
I have a little problem with ereg (eregi) in PHP - in some cases it behaves differently than I expect and I don`t know if my expectations are strange or there is any "syntax subtlety" I have...
3
by: Dynamo | last post by:
Hi, Thanks to all who replied to original posting.As a direct result of now gaining a better understanding of regular expressions, instead of trying to use other peoples scripts I am now trying to...
1
by: Jim Dawson | last post by:
I was writing a subroutine to extract fields from lines of text when I ran into an issue. I have reproduced this error on Perl 5.8 on AIX, 5.8 on Linux and 5.6 on Windows. ############### CUT...
3
by: news | last post by:
I'm trying to make sure a form has only one or two digits in either of two fields. I looked at php.net and http://www.regular-expressions.info/reference.html and this is what I put together, but...
18
by: yawnmoth | last post by:
Say I have the following script: <? $string = 'test'; if (eregi("^+$",$string)) { echo 'matches!'; } else {
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.