Uchsun
Uchsun

Reputation: 361

How to Filter only Character on PHP String?

I have String Like This

<?php

$str = "3451231 3123 Rotary-moulder";

?>

and I need a Function that Only filter the Character(Alphabet)

and the result is "Rotary-moulder"

I have been googling ang Find this Script

<?php

$str = '06516161 065165161 ini adalah percobaan';
preg_match_all('!\d+!', $str, $matches);


$var = implode(' ', $matches[0]);

echo $var;

?>

Thats Work Fine and the Result is only Number. How can I get Only Alphabet Character?

I'm Still Not Understand how to reverse that function.

Any one can help me?

Im very appreciated Your Answer.

Upvotes: 0

Views: 4677

Answers (2)

Manoj Saini
Manoj Saini

Reputation: 108

<?php
    $str = '06516161 065165161 ini adalah percobaan';
    preg_match_all('!\D+!', $str, $matches);


    $var = implode(' ', $matches[0]);

    echo $var;
?>

You have to replace small letter 'd' with 'D' in preg_match_all. 'D' uses for Any non-digit match.

Upvotes: 1

John Conde
John Conde

Reputation: 219804

Try:

$string = preg_replace('/[^a-z]/i', '', $string);

edit

If you want to keep the - try this:

$string = preg_replace('/[^a-z-]/i', '', $string);

Upvotes: 4

Related Questions