Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Friday, June 17, 2016

Regular expressions doesn't change any text in PHP

Regular expressions doesn't change any text in PHP


I have this code to replace every number (and point) and replace it by and the text

$i');     $mag = preg_replace($buscarRegex, $reemplazo, $cadena);       return $cadena;  }    $string = "1. Krewella - Can't Control Myself  2. Kdrew - Circles  3. Korn Feat. Skrillex & Kill The Noise - Narcissistic Cannibal  4. Netsky - Love Has Gone  5. Example - Midnight Run (Flux Pavilion Remix)  6. Madeon - Finale (Radio Version)  7. Feed Me Vs. Knife Party Vs. Skrillex - My Pink Reptile Party (Maluu's Slice'n'diced Mashup)  8. Krewella & Pegboard Nerds - This Is Not The End  9. Skrillex - Bangarang  10. The Prototypes - Suffocate  11. Ayah Marar - Mind Controller (Cutline Remix)  12. Skrillex Feat. Krewella - Breathe (Vocal Edit)  13. Utah Saints Vs. Drumsound & Bassline Smith - What Can You Do For Me (Tantrum Desire Remix)  14. Nero - Promises (Skrillex & Nero Remix)  15. 20 Florence & The Machine - Cosmic Love (Seven Lions Remix)";    echo reem2(nl2br($string));    ?>  

But it doesn't work, It doesn't change anything:

The output in HTML would be:

1. Krewella - Can't Control Myself

2. Kdrew - Circles

3. Korn Feat. Skrillex & Kill The Noise - Narcissistic Cannibal

4. Netsky - Love Has Gone

5. Example - Midnight Run (Flux Pavilion Remix)

6. Madeon - Finale (Radio Version)

7. Feed Me Vs. Knife Party Vs. Skrillex - My Pink Reptile Party (Maluu's Slice'n'diced Mashup)

8. Krewella & Pegboard Nerds - This Is Not The End

9. Skrillex - Bangarang

10. The Prototypes - Suffocate

11. Ayah Marar - Mind Controller (Cutline Remix)

12. Skrillex Feat. Krewella - Breathe (Vocal Edit)

13. Utah Saints Vs. Drumsound & Bassline Smith - What Can You Do For Me (Tantrum Desire Remix)

14. Nero - Promises (Skrillex & Nero Remix)

15. 20 Florence & The Machine - Cosmic Love (Seven Lions Remix)

What can I do?

Answer by exussum for Regular expressions doesn't change any text in PHP


return $cadena;  

is your problem, Your doing the replace and then throwing the result away and returning the input

return $mag;  

is probably what you meant

in fact your regex is also wrong

function reem2($cadena) {       $buscarRegex = array('/^([0-9]{1,2}\.)(.*)$/m');     $reemplazo = array('\1\2');     $mag = preg_replace($buscarRegex, $reemplazo, $cadena);       return $mag;  }  

seems to be what you want.

Answer by Marc B for Regular expressions doesn't change any text in PHP


Your regex is broken:

/^([0-9]{1}|[.])$/i   ^-- start of line                   ^--- end of line  

you are allowing only for one SINGLE character on a line by itself, so the regex can never match anything.

You probably want something more like this:

/^([\d]+)\./  

which will match any number of digits at the start of the line, which are followed by a single ..

Answer by Orangepill for Regular expressions doesn't change any text in PHP


function reem2($cadena) {       $buscarRegex = array('/^([0-9]+\.)/m'); // changed modifier to multiline     $reemplazo = array('$1'); // changed replacement to a capture offset     return preg_replace($buscarRegex, $reemplazo, $cadena);  }  

Answer by Karo for Regular expressions doesn't change any text in PHP


So from what I see, your regex is incorrect and you're returning the incorrect variable in function reem2, so try replacing your function with something like this

function reem2($cadena) {      return preg_replace("/([0-9]+\.)/", "$1", $cadena);  }  

Answer by HamZa for Regular expressions doesn't change any text in PHP


You may use the following code:

function reem2($cadena) {     $buscarRegex = array('/^(\d+\.)/mi'); // This means match any digit(s) followed by a dot at the beginning of each line. Note the m modifier     $reemplazo = array('$1'); // replace should be with group 1, not some vague $i     $mag = preg_replace($buscarRegex, $reemplazo, $cadena);     return $mag; // return value: fixed  }  


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.