Jump to content

make words bold


The Little Guy

Recommended Posts

I have this regexp:

"/function(.+?)\(/"

 

I want to replace the text that is inside the parenthesis.

 

Here is an example string:

function myFunction(){
    alert("hi");
}

 

I want to replace myFunction with html to make it bold

 

Here is what I have, but it is also making the word "function" and "(" bold as well, which I don't want

preg_replace("/function(.+?)\(/", '<span style="font-weight:bold;">$0</span>', $string)

 

What can I do to just make "myFunction" bold?

Link to comment
Share on other sites

Your logic is kinda wrong. You want everything inside the curly brackets to be bold, not the function name, right?

 

Use this regex instead:

 

$string = preg_replace('#{([^}]+)#', '{<span style="font-weight:bold;">$1</span>', $string);

Link to comment
Share on other sites

Either look into positive look-behind assersions,  or cheat and do it the easy way, capture the word function as it's own capture group and make the replacement..

$1<span style="font-weight:bold;">$2</span>

Link to comment
Share on other sites

Here is the method I went with:

"/(function)(.+?)(\()/"

'$1<span style="font-weight:bold;">$2</span>$3'

 

I would like to try the positive look-behind assertion method, so I gave that a try, and came up with this:

"/function(?<=.+?)/"

 

but I get this error:

 

Warning: preg_replace() [function.preg-replace]: Compilation failed: lookbehind assertion is not fixed length at offset 17 in /home/ryannaddy/beta.phpsnips.com/test.php on line 29

 

what could I do to fix it?

Link to comment
Share on other sites

The error is mostly self-explanatory.  You can only do lookbehinds if the pattern you are matching is a fixed length.  So for instance, you can lookbehind for "bob" but not "b[^ ]*". 

 

The main principle to understand here is that when you want to replace one thing with another, since preg_replace replaces everything that you match, you have to account for the "anchor" or "delimiter" parts of your pattern you use to isolate what you're trying to replace.  So for instance, you are identifying the function name by matching for the "function" before it, and the "(" after it.  Those are the "anchor/delimiter" parts of your pattern, what you use to identify the part you're trying to replace.

 

You can accomplish it in several different ways.  One way is to use zero-width assertion, which is basically a fancy way of saying match but do not actually consume or make part of the matched pattern.  This will allow you to not have to worry about the "anchor/delimiter" parts of your pattern.  The problem with that though is that look-behinds must be a fixed length.  In your particular case, you can "probably" get away with using "function " as the look-behind pattern.  But if you wanted to account for arbitrary space between "function" and the function name, it would no longer work, so IMO, using look-behind is not really the "safest" way to go.

 

In general, the "safest" way to go is to just wrap your "anchor/delimiter" portions of your pattern in their own captured groups, and then use them in your replacement argument.  So you mostly had it right in the first place.  You just need to do a little bit of extra capturing, and use the right capture variables.  Also, I changed it to use a negative char class instead of lazy match all because it's more efficient.

 

 

$string = "function blah () {";
$string = preg_replace("~(function\s+)([^(]+)~i", '$1<span style="font-weight:bold;">$2</span>', $string);

Link to comment
Share on other sites

Okay, I removed the validation, since you feel it shouldn't be in a highlighter.

 

Thanks for your guy's help it is awesome!

 

 

Off topic:

 

I downloaded the php source, hoping I could see how highlight_string works, but I can't seem to find the function in the source code, any suggestions on where I can find it?

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.