can any one help me in string padding.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
itsmani1
Forum Regular
Posts: 791
Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:

can any one help me in string padding.

Post by itsmani1 »

I need help in string padding....
my input is :

Code: Select all

$input = "Alien Is my friend, and I like mango.";
now i want to padd all i's and I's with "_" on both sides.
means i want output like this:

Code: Select all

$outut = "Al_i_en _I_s my fr_i_end, and _I_ l_i_ke mango.";
User avatar
itsmani1
Forum Regular
Posts: 791
Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:

Post by itsmani1 »

Code: Select all

if (preg_match ("/php/i", "PHP is the web scripting language and php is my 1st choice.")) {
    print "A match was found.";
} else {
    print "A match was not found.";
}
in the above example i can check the string now how can i padd "_" on both sides of all "php"'s
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

OR! you can just do this

Code: Select all

$string= "Alien Is my friend, and I like mango.";
$string = str_replace('i', '_i_', $string);
$string = str_replace('I', '_I_', $string);
echo $string;
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

You can find many more interesting functions to work with strings at the PHP Manual Strings page.
User avatar
itsmani1
Forum Regular
Posts: 791
Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:

Post by itsmani1 »

shiznatix wrote:OR! you can just do this

Code: Select all

$string= "Alien Is my friend, and I like mango.";
$string = str_replace('i', '_i_', $string);
$string = str_replace('I', '_I_', $string);
echo $string;
is there any better solution than the above one using reguller expression.....


thanx.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

itsmani1 wrote:
shiznatix wrote:OR! you can just do this

Code: Select all

$string= "Alien Is my friend, and I like mango.";
$string = str_replace('i', '_i_', $string);
$string = str_replace('I', '_I_', $string);
echo $string;
is there any better solution than the above one using reguller expression.....


thanx.
For a simple replacement like that it's actually faster (by quite a lot) to use a couple of str_replace()s than one regular expression.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Code: Select all

function padd_at_string($input, $str)
{
    $escaped = preg_quote($str);
    $re = '/('.$escaped.')/';
    return preg_replace($re, '_$1_', $input);
}
EDITTED 3 times :P Too early :(
User avatar
itsmani1
Forum Regular
Posts: 791
Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:

Post by itsmani1 »

d11wtq wrote:

Code: Select all

function padd_at_string($input, $str)
{
    $escaped = preg_quote($str);
    $re = '/('.$escaped.')/';
    return preg_replace($re, '_$1_', $input);
}
EDITTED 3 times :P Too early :(

thanx. man for your time but can you do me one more favour. this don't work with case in-sensitive. can u solve this .
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

itsmani1 wrote:thanx. man for your time but can you do me one more favour. this don't work with case in-sensitive. can u solve this .
Your signature: "Home Page Of AM Solutions :: We Solve Problems". ;)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Code: Select all

function padd_at_string($input, $str)
{
    $escaped = preg_quote($str);
    $re = '/('.$escaped.')/i';
    return preg_replace($re, '_$1_', $input);
}
Post Reply