some php advice needed for string replacement

Post non-phpwcms related topics here - but I don't want to see "hey check this or that other cms". Post if you have a point or worthwhile comment, don't post just to increase you post count!
Post Reply
User avatar
update
Moderator
Posts: 6455
Joined: Mon 10. Jan 2005, 17:29
Location: germany / outdoor

some php advice needed for string replacement

Post by update »

EDIT (did a wrong description of the problem)

Hi,
I want to rewrite a string
my-new-domain.tld
into
my_new_domain_tld
Now when I do
str_replace(".","_", $domain);
only "." are changed into "_" but I have to replace "-" --> "_" (if any) too
Now I'm stuck (didn't buy a book until now and still am now php wizard ) :wink:
Perhaps someone of the more advaned ones could show me the way? This would be great and 8)
Thank you!

Greetings
claus
pepe
Posts: 3954
Joined: Mon 19. Jan 2004, 13:46

Re: some php advice needed for string replacement

Post by pepe »

maybe such a construct:

.
.
$domain1 = str_replace(".","_", $domain);
$domain = str_replace("-","_", $domain1);
.
.
User avatar
Jensensen
Posts: 3000
Joined: Tue 17. Oct 2006, 21:11
Location: auf der mlauer

Post by Jensensen »

Hi claus,

schon mal versucht:

Code: Select all

$badchr		= array("-", ".");
$goodchr	  = array('_', '_');
       
str_replace($badchr, $goodchr, $domain);
greetz
{so_much} | Knick-Knack. | GitHub
Umlaute im URL sind meistens immer Kacke.
User avatar
update
Moderator
Posts: 6455
Joined: Mon 10. Jan 2005, 17:29
Location: germany / outdoor

Post by update »

Danke!
Ja, das hatte ich nachts auch schon überlegt und vorhin alle Vorschläge ausprobiert, leider ohne Erfolg. Dies liegt augenscheinlich aber nicht mehr an dem str_replace, sondern an anderen (Datenbank-)Ungereimtheiten -
ich forsche weiter. Ist ja mächtig spannend!
Gruss
claus
User avatar
update
Moderator
Posts: 6455
Joined: Mon 10. Jan 2005, 17:29
Location: germany / outdoor

Post by update »

ich hab's jetzt so gemacht:

Code: Select all

list($dom, $rd) = explode('.',$domain);
$dom = str_replace(array('.','-'),"_", $dom);
Ob das so super elegant oder gar alles richtig ist, weiss ich nicht, aber es tutet
Danke noch mal für Eure Mühe
Gruss
claus
Post Reply