---
title: "Byta ut tecken."
type: "forum-thread"
url: "https://www.webforum.nu/amne/php/111623-byta-ut-tecken"
topic: "PHP"
topic_url: "https://www.webforum.nu/amne/php"
author: "andsj0"
published: "2004-09-15T13:30:40.000Z"
updated: "2004-09-15T15:24:33.000Z"
replies: 4
views: 327
page: 1
pages: 1
language: "sv-SE"
site: "webForum — webforum.nu"
rights: "Upphovsrätten till varje inlägg tillhör dess författare."
attribution: "Citera som: webForum, https://www.webforum.nu/amne/php/111623-byta-ut-tecken"
---

# Byta ut tecken.

## #1 — andsj0, 2004-09-15T13:30Z

När jag hämtar upp lite info från våran databas så skulle jag vilja byta ut alla siffror 0-9 med exempelvis en stjärna \* All annan info ska visas som vanligt.

Jag tror att det ska gå att göra detta med hjälp av php.

Permalänk: https://www.webforum.nu/p/111623

## #2 — Pedda, 2004-09-15T13:54Z

Här är en funktion

```php
function replaceNummer($text) { 
	$text = str_replace("0", "*", $text); 
	$text = str_replace("1", "*", $text); 
	$text = str_replace("2", "*", $text); 
	$text = str_replace("3", "*", $text); 
	$text = str_replace("4", "*", $text); 
	$text = str_replace("5", "*", $text); 
	$text = str_replace("6", "*", $text); 
	$text = str_replace("7", "*", $text); 
	$text = str_replace("8", "*", $text); 
	$text = str_replace("9", "*", $text); 
	return ($text); 
}
```

Permalänk: https://www.webforum.nu/p/1440230

## #3 — andsj0, 2004-09-15T14:17Z

Jag har provat men inte fått det att funka. Testade på följande sätt men fick bara siffrorna utskrivna.

```

$text = "0123456789";
function replaceNummer($text) { 
    $text = str_replace("0", "*", $text); 
    $text = str_replace("1", "*", $text); 
    $text = str_replace("2", "*", $text); 
    $text = str_replace("3", "*", $text); 
    $text = str_replace("4", "*", $text); 
    $text = str_replace("5", "*", $text); 
    $text = str_replace("6", "*", $text); 
    $text = str_replace("7", "*", $text); 
    $text = str_replace("8", "*", $text); 
    $text = str_replace("9", "*", $text); 
    return ($text); 
}
echo"$text";
```

Tänker jag galet ??

Permalänk: https://www.webforum.nu/p/1440256

## #4 — supreme, 2004-09-15T14:21Z

Det du inte gör är att du glömmer att kalla på funktionen replaceNummer()!

lägg följande kod efter defenitionen av funktionen och innan echo $text;

```php
replaceNummer($text);
```

Permalänk: https://www.webforum.nu/p/1440260

## #5 — Hansen, 2004-09-15T15:24Z

> **Pedda skrev:**
>
> Här är en funktion
>
> ```php
> function replaceNummer($text) { 
> 	$text = str_replace("0", "*", $text); 
> 	$text = str_replace("1", "*", $text); 
> 	$text = str_replace("2", "*", $text); 
> 	$text = str_replace("3", "*", $text); 
> 	$text = str_replace("4", "*", $text); 
> 	$text = str_replace("5", "*", $text); 
> 	$text = str_replace("6", "*", $text); 
> 	$text = str_replace("7", "*", $text); 
> 	$text = str_replace("8", "*", $text); 
> 	$text = str_replace("9", "*", $text); 
> 	return ($text); 
> }
> ```

Här är betydligt kortare variant, mer effektiv:

```
<?php

function replaceNumber($string) {
    return preg_replace( '/\d/', '*', $string ); 
}

echo replaceNumber('A0123456789B'), "\n";
?>
```

Permalänk: https://www.webforum.nu/p/1440300

---

Tråden på webben: https://www.webforum.nu/amne/php/111623-byta-ut-tecken
