AJFMEDIA
AJFMEDIA

Reputation: 2123

remove year (yyyy) from string in php

I cant get the regex to identify the date from a string and remove it with string replace:

<?php

$string = "keywords=2012+some+words";

echo $string ."<br />";

$new_string = str_replace("keywords=/^([0-9]{4})$/","keywords=",$string);

echo $new_string ;
?>

I have read this post Regex to remove year from a string PHP

and just cant get it to work

Upvotes: 2

Views: 1653

Answers (1)

Joseph Silber
Joseph Silber

Reputation: 219938

To use a regex, use preg_replace, and pass it an actual regex:

$new_string = preg_replace("/^keywords=\d{4}/", "keywords=", $string);

See it here in action: http://codepad.viper-7.com/dxwxvG

Upvotes: 3

Related Questions