2 Replies - 5919 Views - Last Post: 24 July 2008 - 02:06 PM Rate Topic: -----

#1 bstonehill  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 113
  • Joined: 21-January 08

Strip characters from a phone number using trim()

Posted 24 July 2008 - 01:02 PM

I'm trying to strip the hyphens out of a posted phone number field so that I can re-format them uniformly whether the user uses hyphens or not. I've used trim() before without much trouble but for some reason it's not stripping out the characters.

	function formatPhone($ph){
		$trimmed = trim($ph, "-");
		$phA = substr($trimmed, 0, 3);
		$phB = substr($trimmed, 3, 3);
		$phC = substr($trimmed, 6);
		return $phA."-".$phB."-".$phC;
	}
	
	$phone = formatPhone($_POST['phone']);
	$contact_phone = formatPhone($_POST['contact_phone']);



Is This A Good Question/Topic? 0
  • +

Replies To: Strip characters from a phone number using trim()

#2 Moonbat  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 36
  • View blog
  • Posts: 424
  • Joined: 30-June 08

Re: Strip characters from a phone number using trim()

Posted 24 July 2008 - 01:29 PM

Why don't you just use str_replace and do something like:

str_replace("-", "", $thephonenumber

It'll replace the hyphen with nothing, in other words, it'll get rid of the hyphens.

This post has been edited by Moonbat: 24 July 2008 - 01:30 PM

Was This Post Helpful? 1
  • +
  • -

#3 bstonehill  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 113
  • Joined: 21-January 08

Re: Strip characters from a phone number using trim()

Posted 24 July 2008 - 02:06 PM

View PostMoonbat, on 24 Jul, 2008 - 04:29 PM, said:

Why don't you just use str_replace and do something like:

str_replace("-", "", $thephonenumber

It'll replace the hyphen with nothing, in other words, it'll get rid of the hyphens.


That did the trick, thanks!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1