Jump to content

preg_replace Help


Cetanu
 Share

Recommended Posts

I have a simple (?) preg_replace statement that I would like to replace a { with a <, but I keep getting it wrong and right now RegEx really confuses me, so help is appreciated. :D

 

		preg_replace("/(\{.*)/",  "<", $message); 

 

Thanks in advance.

Link to comment
Share on other sites

Understand that the star quantifier (*) is greedy.. so odds are, its capturing more than you want.. perhaps without seeing an example, the best guess I throw at the moment would be to make it lazy, so .* becomes .*? (but I suspect there might be more to it - perhaps showing a sample string?)

 

[ot]

I'm not sure you need to use a capture either (you can eliminate the parenthesis). Also, you don't need to esacpe the { character in your pattern.

[/ot]

Link to comment
Share on other sites

This is a function similar to BBCode. I would like something like "{u}Hello{/u}" to be changed into <u>Hello</u>.

 

The code snippet I showed is just for left bracket, but I assumed I could go from there after learning what I'm doing wrong.

Link to comment
Share on other sites

I've checked that code outside of my file, and it worked. Inside of my file, it does not and I don't know why.

 

if(!empty($_POST['submit'])){
		connect($db="government"); 
		preg_replace('#\{{1,1}(.*?)\}{1,1}#',"<$1>", $message);
		echo $message;
		echo "<script>alert('Success!'); location='editCont.php?page=editNews';</script>"; 
	}

Link to comment
Share on other sites

@Cetanu

 

Do a forum search on the topic of regex and bbc, as this is a re-occuring theme... I don't know the threads off hand, but this has been asked (and answered) plenty of times before.

 

@rajivgonslaves

declaring intervals like {1,1} is not terribly useful, as it is the same thing as the character(s) that precedes it (only I would wager this would be slower). So doing something like 'a{1,1}' is the same thing as simply stating 'a'. Also note that you don't need to escape the curly braces (it's a common misconception - there might be some circumstances where it might be needed, but I'm finding that in general, it isn't).. your pattern could be more along the lines of:

 

#{(.*?)}#

 

[ot]

Granted, either way, patterns like this could theoritically match '{I am a string inside curly braces} and alter that.. depending on the OP's circumstances, this may not be desirable. Again, there are threads within this forum that discuss bbc tag parsing.

[/ot]

Link to comment
Share on other sites

This thread is more than a year old.

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.

 Share

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