Jump to content

preg_replace Help


Cetanu

Recommended Posts

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
https://forums.phpfreaks.com/topic/185026-preg_replace-help/#findComment-976720
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
https://forums.phpfreaks.com/topic/185026-preg_replace-help/#findComment-976754
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
https://forums.phpfreaks.com/topic/185026-preg_replace-help/#findComment-976766
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
https://forums.phpfreaks.com/topic/185026-preg_replace-help/#findComment-976850
Share on other sites

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.