Jump to content

Searching and replacing with Variables


Daleeburg

Recommended Posts

<?php

/* Method 1 - This method replaces ALL numbers with $replacement */

$replacement = 1;
$string = "hello-123";

echo preg_replace("/(\d+)/", $replacement, $string);

/* Method 2 - This method just replaces with the syntax [string]-[number] */

$replacement = 1;
$string = "hello-123";

$pieces = explode("-", $string);
$pieces[1] = $replacement;

echo join("-", $pieces);

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.