Connecting Tech Pros Worldwide Forums | Help | Site Map

substr/ereg question

MIchel
Guest
 
Posts: n/a
#1: Jul 17 '05
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



J.O. Aho
Guest
 
Posts: n/a
#2: Jul 17 '05

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
MIchel
Guest
 
Posts: n/a
#3: Jul 17 '05

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?


J.O. Aho
Guest
 
Posts: n/a
#4: Jul 17 '05

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
Janwillem Borleffs
Guest
 
Posts: n/a
#5: Jul 17 '05

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



Janwillem Borleffs
Guest
 
Posts: n/a
#6: Jul 17 '05

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



Janwillem Borleffs
Guest
 
Posts: n/a
#7: Jul 17 '05

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



Michel
Guest
 
Posts: n/a
#8: Jul 17 '05

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]


Jerry Sievers
Guest
 
Posts: n/a
#9: Jul 17 '05

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/
Evert
Guest
 
Posts: n/a
#10: Jul 17 '05

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
Closed Thread


Similar PHP bytes