Jump to content

PHP: replace the start & end of string using regular expressions & preg_replace(


justravis

Recommended Posts

thanks for reading my thread.

 

isnt ^ common regex notation?

 

keep getting this error:

Parse error: syntax error, unexpected '^'

 

$goalsheet.=preg_replace("/^[0-9]+ Year Career Goal #[0-9]+: *$\", "/^<span class=hd2>[0-9]+ Year Career Goal #[0-9]+: *</span><br>$/", $goallist);

 

aside from the error, i think the regex is off, but i hav had a hard time understanding how I can ignore text in the middle.

 

Here are some examples of what i'm trying to do:

"1 Year Goal #1: Web Developer" -> "<span>1 Year Goal #1: Web Developer</span><br>"

"10 Year Goal #2: IT Consultant" -> "<span>10 Year Goal #2: IT Consultant</span><br>"

 

suggestions?

try

<?php
$goallist = '1 Year Goal #1: Web Developer';
//$goallist = '10 Year Goal #2: IT Consultant';
echo $goalsheet = preg_replace("/^([0-9]+) Year Goal #([0-9]+: .*)$/", "<span class='hd2'>$1 Year Career Goal #$2</span><br>", $goallist);
?>

or

$goalsheet = "<span class='hd2'>" . $goallist . "</span><br>";

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.