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?

Link to comment
Share on other sites

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>";

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.