Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Pro PHP
| Search | Today's Posts | Mark Forums Read
Pro PHP Advanced PHP coding discussions. Beginning-level questions will be redirected to the Beginning PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old August 11th, 2003, 07:49 AM
Friend of Wrox
Join Date: Jun 2003
Location: , , USA.
Posts: 101
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Moharo
Default string operations

i'm creating a functions that will remove and add brackets at the beginning and end of the string. what is the best function to remove the character from a string. i was thinking about using ltrim() and rtrim OR str_replace()... which is more efficient? (see code below):


function remove_brackets($element) {
    $last_char = strlen($element) - 1;
    if(strpos($element,"{") == 0 && strpos($element,"}") == $last_char) {
        $element = ltrim($element,"{");
        $element = rtrim($element,"}");
        return $element; }

$string = "1,{2:4:5},2,{6:8:10},3,{11:14}";
$string = split(",",$string);

echo remove_brackets($string[1]);


thanx in advance :D:D

the genuine genius
www.campusgrind.com - college portal
Old August 14th, 2003, 01:28 AM
Authorized User
Join Date: Aug 2003
Location: , , .
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts

first your remove_bracket function should first test if ($last_char > 0).
it could prevent some errors but also make your function more efficient in case it wouldn't work anyway.

about the strReplace or lTrim/rTrim :
in both ways, you will have to run two functions :
in one case : strReplace "{" and strReplace "}"
in the other : lTrim and rTrim

all those function are php standard so i guess we can trust the developers on having optimized their code.
my opinion would be that the lTrim/rTrim should be more efficient, because it won't parse the whole string as strReplace would (and twice), but strReplace would remove all your brackets in any position within your string. depends on what you want.

to conclude, about your function, you shouldn't test the existence of brackets the way you do.
what you're doing is :
are the brackets in the first AND last position of the string ?
but you should do:
are my first AND last characters brackets ?
not that it would be faster (maybe it would, i don't know), but it should really avoid some errors...

php/java developer
NTIC engineer
Old August 18th, 2003, 01:06 PM
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Jun 2003
Location: San Diego, CA, USA
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts

Another suggestion:

$len = strlen($str);
if(($len > 1) && // must be at least two chars long: "{}"
   ($str[0] == '{') &&
   ($str[$len - 1] == '}'))
  $str = substr($str, 1, $len - 2);

This way, you have a simple way of testing that BOTH end characters are the proper braces, and you use substr() to trim the ends off.

Take care,


Similar Threads
Thread Thread Starter Forum Replies Last Post
File I/O Operations in C++ code_lover C++ Programming 1 August 10th, 2008 12:16 PM
Preventing operations with Hourglass Mousepointer krithi20 Pro VB 6 5 August 23rd, 2005 04:50 PM
Table operations Paaji Beginning VB 6 0 August 14th, 2005 03:22 PM
timespan operations marclena General .NET 1 February 25th, 2005 07:08 AM
2 ListBox operations melvik C# 1 August 19th, 2003 06:51 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.