TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 04-08-2008, 12:56 AM   #1 (permalink)
The Contributor
 
Join Date: Apr 2008
Posts: 78
Thanks: 0
benton is on a distinguished road
Default Looking for a better way to code this

I'm building a string dynamically. The result is that it may start with a dash (-), a space or a letter. I need to remove the first two, if they occur. The following code simulates the problem. If ran as is, it outputs XB - CX, which is correct (the X's are to show there aren't any spaces). But it seems unnecessary and overly complicated to use three trims in a row. The $array can come from a number of sources so while I could probably condition the string there, that would cause more code than the three trims. Can anyone see a better way to do this?

PHP Code:
$array = array();
$array['a'][0] = '';
$array['a'][1] = ' B';
$array['a'][2] = 'C';

$str implode(' ' .' - ' ' '$array['a']);
$str trim($str);
$str ltrim($str'-');
$str ltrim($str);
echo 
'X'.$str.'X'
benton is offline  
Reply With Quote
Old 04-08-2008, 03:26 AM   #2 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

PHP Code:
$array['a'][0] = '';
$array['a'][1] = ' B';
$array['a'][2] = 'C-';

$str trim(implode(' - '$array['a']), " -");
echo 
'X'.$str.'X'
should do the trick.
delayedinsanity is offline  
Reply With Quote
Old 04-08-2008, 12:10 PM   #3 (permalink)
The Contributor
 
Join Date: Apr 2008
Posts: 78
Thanks: 0
benton is on a distinguished road
Default

Hmm, I had tried something like that, although with ltrim. Maybe I'm confused about the charlist argument. I tried
PHP Code:
ltrim($str"' '..'-') 
which seems to be the way the manual says to enter multiple characters. Is that not correct? Isn't the ' -' in yours just searching for that combination (space and dash)? I can see the code works but would like to understand the correct use of the command.
benton is offline  
Reply With Quote
Old 04-08-2008, 02:10 PM   #4 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

What it means by "With .. you can specify a range of characters." is a range of characters such as 1 to 9, or A to C, etc (1..9, or A..C, etc). For the basic characters you wish stripped, simply list one right after another, such as " -" or " \t_" etc. Nothing to seperate them, PHP will look at the characters one by one unless it sees .. to designate a range.
-m
delayedinsanity is offline  
Reply With Quote
Old 04-09-2008, 02:25 AM   #5 (permalink)
The Contributor
 
Join Date: Apr 2008
Posts: 78
Thanks: 0
benton is on a distinguished road
Default

Thank you for the explanation. I thought they were making more complicated than needed when I was trying that. Now it all makes sense.
benton is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 05:47 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design