FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Syntax for trim charlist?
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
Syntax for trim charlist? [message #171935] Fri, 21 January 2011 23:35 Go to next message
GarryJones is currently offline  GarryJones
Messages: 21
Registered: September 2010
Karma: 0
Junior Member
I need to trim away * and # in a list of names.

First I want to get rid of the standard funny stuff so I do this.

$mytrim1 = trim($mytext);

Now I need to remove all the * and the # and this is where I am stuck.

The syntax on php.net says to use "string trim ( string $str [, string
$charlist ] )"

As in $mytrim2 = trim($mytrim2, $charlist);

Countless complex examples are given but nowhere I can find an
explantion of how to write $charlist - I just need to trim all the *
and the # from this list of names.

So bottom line, how do I set $charlist to just omit the * and the #

Can these characters be added to the first "standard" trim to cut the
code down to one row?

And question 2. Is there a "php made simple" website. I just wanted a
simple standard explanation. I find I am always looking at massive
complex solutions in the examples on php.net. All very useful I am
sure but I need to learn to walk before I can run.

Any help greatly appreciated.

Garry Jones
Sweden
Re: Syntax for trim charlist? [message #171936 is a reply to message #171935] Sat, 22 January 2011 00:20 Go to previous messageGo to next message
GarryJones is currently offline  GarryJones
Messages: 21
Registered: September 2010
Karma: 0
Junior Member
Oops

Error in my post

For "As in $mytrim2 = trim($mytrim2, $charlist);"
Read "As in $mytrim2 = trim($mytrim1, $charlist);"

Cheers!
Re: Syntax for trim charlist? [message #171937 is a reply to message #171935] Sat, 22 January 2011 01:08 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On 21/01/11 23:35, GarryJones wrote:

> I need to trim away * and # in a list of names.

Assuming the list is a string or array, and that I have correctly
understood the requirement:

<?php
$destination = preg_replace("/[#*]+/g","",$source);
// $destination contains the modified version of $source
?>

or:

<?php
$source = preg_replace("/[#*]+/g","",$source);
// $source is replaced with filtered $source
?>

Rgds

Denis McMahon
Re: Syntax for trim charlist? [message #171939 is a reply to message #171937] Sat, 22 January 2011 01:52 Go to previous messageGo to next message
GarryJones is currently offline  GarryJones
Messages: 21
Registered: September 2010
Karma: 0
Junior Member
> $source = preg_replace("/[#*]+/g","",$source);
> // $source is replaced with filtered $source

I can't get this example to work.

$source = "garry *** ";
$source = preg_replace("/[#*]+/g","",$source);

print $source;

.....returns an empty string

I need to remove all the * and all the # from the string.

In the records the * and the # are entered after names, sometimes with
a following space

Example of records that are to be treated with this script...

record 1 "Garry ### "
record 2 "John * "
record 3 "Anna ********"
record 4 "Fred **#*###**"

ie, it can be any number of * and # and they can be followed by a
space or not.

I intend to treat every record one by one within a "WHILE", I know how
to that....

For the above I need the output...
"Garry"
"John"
"Anna"
"Fred"

Hope I have explained this better now....

I think I can use "trim" as in $mytrim2 = trim($mytrim1, $charlist);"

And that what I need to do is to declare the variable "$charlist" so
it trims all * and # but I am stuck on how to set $charlist

Garry Jones
Sweden
Re: Syntax for trim charlist? [message #171940 is a reply to message #171935] Sat, 22 January 2011 02:01 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On 21/01/11 23:35, GarryJones wrote:

> I need to trim away * and # in a list of names.

In a list, or from the start and end of a list.

trim only removes matched characters at the start or end of a string.

eg if you want to convert

"fred * jim ## susan" to "fred jim susan" trim will not work, as the *
and # characters are not at the ends of the string.

This line on the php.net documentation for trim is very important:

"trim — Strip whitespace (or other characters) from the beginning and
end of a string"

Note specifically that it ends "from the beginning and end of a string"

Rgds

Denis McMahon
Re: Syntax for trim charlist? [message #171941 is a reply to message #171939] Sat, 22 January 2011 02:16 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On 22/01/11 01:52, GarryJones wrote:
>> $source = preg_replace("/[#*]+/g","",$source);
>> // $source is replaced with filtered $source
>
> I can't get this example to work.
>
> $source = "garry *** ";
> $source = preg_replace("/[#*]+/g","",$source);
>
> print $source;
>
> ....returns an empty string

Oops, doesn't need the g modifier.

Try:

$source = preg_replace("/[#*]+/","",$source);

> record 1 "Garry ### "
> record 2 "John * "
> record 3 "Anna ********"
> record 4 "Fred **#*###**"

<?php
$recs = array("Garry ### ","John * ","Anna ********","Fred **#*###**");
foreach ($recs as $str) print trim($str, " \t\n\r\0\x0B*#") . "\n";
?>

Garry
John
Anna
Fred

Rgds

Denis McMahon
Re: Syntax for trim charlist? [message #171944 is a reply to message #171935] Sat, 22 January 2011 03:09 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 1/21/2011 6:35 PM, GarryJones wrote:
> I need to trim away * and # in a list of names.
>
> First I want to get rid of the standard funny stuff so I do this.
>
> $mytrim1 = trim($mytext);
>
> Now I need to remove all the * and the # and this is where I am stuck.
>
> The syntax on php.net says to use "string trim ( string $str [, string
> $charlist ] )"
>
> As in $mytrim2 = trim($mytrim2, $charlist);
>
> Countless complex examples are given but nowhere I can find an
> explantion of how to write $charlist - I just need to trim all the *
> and the # from this list of names.
>
> So bottom line, how do I set $charlist to just omit the * and the #
>
> Can these characters be added to the first "standard" trim to cut the
> code down to one row?
>
> And question 2. Is there a "php made simple" website. I just wanted a
> simple standard explanation. I find I am always looking at massive
> complex solutions in the examples on php.net. All very useful I am
> sure but I need to learn to walk before I can run.
>
> Any help greatly appreciated.
>
> Garry Jones
> Sweden

It's just a list of characters you want trimmed from the front and end
of a string, i.e.

$str1 = '#*It works *# #* *#';
$str2 = trim($str1, '#* ');
echo $str;

prints "It works"

Be sure to add a space character in case you have one at the end. And
while you can use a regex, it's way overkill for something this simple.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: Syntax for trim charlist? [message #171945 is a reply to message #171935] Sat, 22 January 2011 03:15 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 1/21/2011 6:35 PM, GarryJones wrote:
> I need to trim away * and # in a list of names.
>
> First I want to get rid of the standard funny stuff so I do this.
>
> $mytrim1 = trim($mytext);
>
> Now I need to remove all the * and the # and this is where I am stuck.
>
> The syntax on php.net says to use "string trim ( string $str [, string
> $charlist ] )"
>
> As in $mytrim2 = trim($mytrim2, $charlist);
>
> Countless complex examples are given but nowhere I can find an
> explantion of how to write $charlist - I just need to trim all the *
> and the # from this list of names.
>
> So bottom line, how do I set $charlist to just omit the * and the #
>
> Can these characters be added to the first "standard" trim to cut the
> code down to one row?
>
> And question 2. Is there a "php made simple" website. I just wanted a
> simple standard explanation. I find I am always looking at massive
> complex solutions in the examples on php.net. All very useful I am
> sure but I need to learn to walk before I can run.
>
> Any help greatly appreciated.
>
> Garry Jones
> Sweden

Another thought - if you need to replace the characters throughout the
string, instead of just beginning and end, use str_replace, i.e.

$str1 = '#*It *##*works *# #* *#';
$str2 = str_replace(array('#', '*'), '', $str1);
echo $str;

also prints "It works"

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: Syntax for trim charlist? [message #171993 is a reply to message #171945] Wed, 26 January 2011 10:08 Go to previous message
Curtis Dyer is currently offline  Curtis Dyer
Messages: 34
Registered: January 2011
Karma: 0
Member
Jerry Stuckle <jstucklex(at)attglobal(dot)net> wrote:

> On 1/21/2011 6:35 PM, GarryJones wrote:
>> I need to trim away * and # in a list of names.
>>
>> First I want to get rid of the standard funny stuff so I do
>> this.
>>
>> $mytrim1 = trim($mytext);
>>
>> Now I need to remove all the * and the # and this is where I am
>> stuck.

<snip>

>> So bottom line, how do I set $charlist to just omit the * and
>> the #
>>
>> Can these characters be added to the first "standard" trim to
>> cut the code down to one row?

<snip>

> Another thought - if you need to replace the characters
> throughout the string, instead of just beginning and end, use
> str_replace, i.e.
>
> $str1 = '#*It *##*works *# #* *#';
> $str2 = str_replace(array('#', '*'), '', $str1);
> echo $str;

ITYM "echo $str2;"

If you want to replace "#" and "*" throughout the string /and/ to
trim extraneous whitespace at the beginning or end of your string,
you can call trim() on the string returned by str_replace():

$str1 = ' *foo* bar## ';
$str2 = trim(str_replace(array('#','*'), '', $str1));
echo $str2;

--
Curtis Dyer
<?$x='<?$x=%c%s%c;printf($x,39,$x,39);?>';printf($x,39,$x,39);?>
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Implement Php performace
Next Topic: Is there any situation where anything other than require_once is better?
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Thu Dec 12 14:15:01 GMT 2024

Total time taken to generate the page: 0.01992 seconds