Forum Moderators: coopster

Message Too Old, No Replies

preg replace thows an unexpected T Variable error

         

neophyte

11:03 am on May 23, 2007 (gmt 0)

10+ Year Member



Hello All -

I'm drawing some pre-HTML formatted content from a DB table.

The result set that resides in a $content VAR looks something like this:

<h1>Crew Member Applicant</h1>

<h2>Personal Information Section</h2>

<p>Blah, blah, blah...</p>

On a few pages of this application, I need to slip a position name just after the Crew Member Application line so it'll look something like this: "Crew Member Application - Captain".

So, I'm trying to use preg_replace - trapping for the closing </h1> tag - like this:

$replace = '</h1>'
$replaceWith = ' - ' . $_SESSION['DesiredPosition'] . '</h1>';
$string = $content;

$content = preg_replace($replace, $replaceWith, $string);

echo $content;

... but, no luck: I keep getting an unexpected T_VARIABLE error.

I suspect it's something to do with the way I'm transferring $content to $string before I do the preg_replace, but I'm not sure.

Could someone please show me the error or my ways?

Great appreciation to all in advance!

Neophyte

barns101

11:18 am on May 23, 2007 (gmt 0)

10+ Year Member



Removing the single quote marks should work:

$replaceWith = ' - ' . $_SESSION[DesiredPosition] . '</h1>';

whoisgregg

1:27 pm on May 23, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Looks like a missing semicolon. :)

$replace = '</h1>'[red][b];[/b][/red] 

neophyte

3:42 am on May 24, 2007 (gmt 0)

10+ Year Member



barns101 and whoisgregg:

Yep, it WAS missing that pesky semicolon - guess I do need glasses. Works like a charm now.

Quick follow-on:

Is preg_replace the best/most efficient function to use for this kind of text replacement task? Would ereg_replace or str_replace be better?

Neophyte

eelixduppy

3:53 am on May 24, 2007 (gmt 0)



str_replace() would definitely be better for a replacement such as this. The needle is static and doesn't need a pattern, so the str_replace would be faster and more efficient. :)