AnchovyLegend
AnchovyLegend

Reputation: 12537

Removing two blacklisted characters from a string

Given the address: 12345 West Palm Rd., #7B Daytona, FL

I am trying to remove the '.' character from 'Rd.' and the '#' character from '#7B'. However, I would like for these characters to be removed no matter what string is given.

Is there an easy way to do this?

I am familiar with strstr(), but it seems like a complicated way to do what I am trying to do.

Upvotes: 2

Views: 245

Answers (6)

mlishn
mlishn

Reputation: 1667

$string = str_replace (array(".", "#"), "", $string);

Upvotes: 8

Tim S
Tim S

Reputation: 5101

$str = preg_replace('/\./', '', $address);
$str = preg_replace('/#/', '', $str);

Upvotes: 1

Fanda
Fanda

Reputation: 3786

Look at example #2 on strtr function doc.

Upvotes: 1

Use the following php code:

$smt="12345 West Palm Rd., #7B Daytona, FL";
$smt=str_replace(".","",$smt);
$smt=str_replace("#","",$smt);

Upvotes: 1

WhoaItsAFactorial
WhoaItsAFactorial

Reputation: 3558

PHP has a convient string replace method for what you are trying to do.

$address = "12345 West Palm Rd., #7B Daytona, FL";
$characters = array('.','#',etc.);
$stripped = str_replace($characters, '', $address);
echo $stripped; //  12345 West Palm Rd, 7B Daytona, FL

Upvotes: 3

rrehbein
rrehbein

Reputation: 4160

$string = strtr($string, array('.' => '', '#' => ''));

Upvotes: 1

Related Questions