substr/ereg question 
July 17th, 2005, 11:29 AM
| | | 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 | 
July 17th, 2005, 11:29 AM
| | | Re: substr/ereg question
MIchel wrote:
[color=blue]
> 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.[/color]
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.
[color=blue]
> a: cut string IF longer than 15 chars.[/color]
if( strlen($name)>15 ) {
[color=blue]
> b: set flag IF string's been cut.[/color]
$shortname=substr($name,0,15);
}
you can test the flag with
if(isset($shortname) {
/* do what ever you want */
}
//Aho | 
July 17th, 2005, 11:29 AM
| | | Re: substr/ereg question
[color=blue]
> if( strlen($name)>15 ) {
>[color=green]
> > b: set flag IF string's been cut.[/color]
>
> $shortname=substr($name,0,15);
>
> }
>
> you can test the flag with
>
> if(isset($shortname) {
> /* do what ever you want */
> }[/color]
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? | 
July 17th, 2005, 11:29 AM
| | | Re: substr/ereg question
MIchel wrote:[color=blue][color=green]
>>if( strlen($name)>15 ) {
>>
>>[color=darkred]
>>>b: set flag IF string's been cut.[/color]
>>
>> $shortname=substr($name,0,15);
>>
>>}
>>
>>you can test the flag with
>>
>>if(isset($shortname) {
>> /* do what ever you want */
>>}[/color]
>
>
> 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?[/color]
don't think so, I fear you would get a true in each case.
//Aho | 
July 17th, 2005, 11:29 AM
| | | Re: substr/ereg question
MIchel wrote:[color=blue]
> 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.
>[/color]
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 | 
July 17th, 2005, 11:29 AM
| | | Re: substr/ereg question
Janwillem Borleffs wrote:[color=blue]
> 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.[/color]
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 | 
July 17th, 2005, 11:29 AM
| | | Re: substr/ereg question
Janwillem Borleffs wrote:[color=blue]
> list($mystring, $result) = preg_split("/(?<=.{15})/", $mystring);
>[/color]
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 | 
July 17th, 2005, 11:30 AM
| | | Re: substr/ereg question
Thank you all very much for your suggestions. I have a working solution now.
Michel
"Janwillem Borleffs" <jw@jwscripts.com> wrote in message
news:420f9e02$0$13795$a344fe98@news.euronet.nl...[color=blue]
> Janwillem Borleffs wrote:[color=green]
> > list($mystring, $result) = preg_split("/(?<=.{15})/", $mystring);
> >[/color]
>
> 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
>
>
>[/color] | 
July 17th, 2005, 11:30 AM
| | | Re: substr/ereg question
"MIchel" <no@spam.please> writes:
[color=blue]
> 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:[/color]
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/ | 
July 17th, 2005, 11:31 AM
| | | Re: substr/ereg question
MIchel wrote:[color=blue]
>
> a: cut string IF longer than 15 chars.
> b: set flag IF string's been cut.
>[/color]
What about :
$shortname = strlen($name)>15?substr($name,0,15):false;
grt,
Evert www.rooftopsolutions.nl | | Thread Tools | Search this Thread | | | |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 220,989 network members.
|