Jump to content
xisto Community
Sign in to follow this  
Amezis

Replace A Character In A Specific Tag

Recommended Posts

I need a code that replaces all spaces between [[ ]] brackets with an underscore.

So, basically, I write this into a variable:

bla bla something [[some text]] more text here

And I want a script to change it to this:
bla bla something [[some_text]] more text here

Any suggestions?

Share this post


Link to post
Share on other sites

Yes! i have a suggestion!!
This may well be the sloppiest suggestion around but it does work and you can easilly use an include to keep the scripts looking tidy. I couldnt think of any other way around it so here is my idea (in hide codes as its long!)
[hide=code]

<?//find string from big string then use search and replace to get rid of spaces in favour of underscores/////////////////////////////////////////////////////////////////////////////////////////////you can delete this line and replace it with something like $string1 = $_POST['string']; or whatever it may //be called And all variable names can be changed just remember to change them all the way through!///////////////////////////////////////////////////////////////////////////////////////////$string1 = $_GET['string'];//first bracket$bracket_1 = strpos($string1, "[");//second bracket$bracket_2 = $bracket_1 + 2; //find first closing bracket to identify the little string to replace spaces within. $c_bracket = strpos($string1, "]", $bracket_2);// get the number of characters between the brackets$chars = $c_bracket - $bracket_2;//now got the bracket's positions we can get this mini string between them$sub = substr($string1, $bracket_2, $chars);//now we can search and replace the spaces :)$no_spaces = str_replace(" ", "_", $sub);//final string with no spacesecho $no_spaces;//shadow-x :) Spread and use as needed!?>
[/hide]

(hide codes added thanks to jlhaslip for lettng me know how!)

Feedback is appreciated by everyone to help me improve! and as in the code if you do use it then its free and give to anyone you want, just dont say you wrote it! Which im sure you wouldnt anyway!
Edited by shadowx (see edit history)

Share this post


Link to post
Share on other sites

Well... you could do all that. Or:
$string = preg_replace('#\[\[(.[^\]]*)\]\]#e', 'str_replace(" ","_","$1");', $string);



HAHA :) me so stupid! Ive only used that function once and it confused me, there is my defence!

either way they will both solve the problem except minew is excessively long :)
Edited by shadowx (see edit history)

Share this post


Link to post
Share on other sites

arent regular expressions just lovely ?


Well for me they are not that great because they cant handle nesting in HTML tags.
Had they been able to do so they would have been wonderful and perfect.

Share this post


Link to post
Share on other sites

A well-written regular expression can match almost anything you through at it, particularly if it follows a certain structure - ie. HTML. What's an example of what you're trying to match, electron?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.