Amezis
Oct 15 2006, 06:02 PM
| | I need a code that replaces all spaces between [[ ]] brackets with an underscore.
So, basically, I write this into a variable:CODE bla bla something [[some text]] more text here
And I want a script to change it to this:CODE bla bla something [[some_text]] more text here
Any suggestions? |
Reply
shadowx
Oct 15 2006, 07:26 PM
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] 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 spaces echo $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!
Reply
Spectre
Oct 15 2006, 08:42 PM
Well... you could do all that. Or: CODE $string = preg_replace('#\[\[(.[^\]]*)\]\]#e', 'str_replace(" ","_","$1");', $string);
Reply
Tsunami
Oct 15 2006, 09:18 PM
arent regular expressions just lovely ?
Reply
shadowx
Oct 16 2006, 12:31 PM
QUOTE 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
Reply
Amezis
Oct 16 2006, 05:42 PM
Thanks a lot! I didn't think about using str_replace inside the preg_replace
Reply
electron
Oct 17 2006, 04:58 AM
QUOTE(Tsunami @ Oct 15 2006, 09:18 PM)  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.
Reply
Spectre
Oct 17 2006, 11:59 AM
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?
Reply
Similar Topics
Keywords : replace, character, specific, tag
- A Simple Preg_replace Help Please.
(1)
Preg Replace Problem
(1) If i have a word like c.........a......t (with the dots in) and i wanted to replace it with cat how
would i do it. If i use CODE <?php $patterns =
"/c(*)a(*)t/"; $replace = "cat"; echo
preg_replace($patterns, $replace, 'c.....a.........t');
[/color][color="#000000"]?> then when i type something like "
c ome a nd look at t his" is also filtered. Is
there any way just to filter cat with dots in? ....
Php Preg Replace
(1) Ive got a problem with preg replace, this is the code: display.php CODE <table
border="1"> <tr><th width="70">Time</th><th
width="200">IP</th><th
width="70">Viewed</th></tr> <?php $LogData =
file_get_contents("log.txt"); $Find =
"/||(.*)|(.*)|(.*)||/i"; $Replace =
"<tr><td>$1</td><td>$2</td><td>$3
</td></tr>"; ....
Directing To A User To Specific Page First Time Only
(3) I make a signup page. I want that when someone signups and login for first time..then he is directed
to the page X automatically. but after that it is redirected to Page Y always... Its pretty simple
thing to do but im not understanding how i can make the script to identify that a person has logged
in for first time ... anyhelp would be so welcome. Thanks.....
Preg_replace Problems
(7) Sigh... preg_replace is getting very annoying for me. Could sombody tell me what I'm doing
wrong? Im making a bbcode replace for the forums I made, and preg_replace is being a b****...
Right now Im working on the tag, and I want to check for valid URL's, because idiots in the
forum are adding js and stuff in the image tags and messing it all up. So i put a function in the
array that holds all of the replacements: CODE
$main_search=array('/\[b\](.*?)\[\/b\]
/is','/\[i\ ....
Help With Preg_replace
(3) I must be a total noob, or just really tired...............-.- either way, I cannot understand what
exactly those funky strings preg_replace wants for the pattern. Could sombody explain to me exactly
what the pattern string and the replace string in preg replace does? I get str_replace...
that's quite basic. I considered myself a pro at php until tonight........haha shows how much i
know. I bet tomorow, if i look it up I'll get it.... Anyway.. any help? Thanks.....
/laugh.gif" style="vertical-align:middle" emoid=":lol:" border="0" alt="laugh.gif" />....
How To Read Specific Row From Table
according where no=xx (5) For example... a table have 2 columns, 100 rows for column i.e. no, name I want to get
corresponding name when no=17 How to write this php coding??....
Help With Reading Files
Read and replace/insert data from form (5) Hi, Does anyone know how i can do this, or scripts that will work on Trap17's servers and will
do the following: I have a .doc file form. Which i want to have filled in automactically, by HTML
Form and emailed to my address, with the data filled in. Any ideas? I have heard many differing
things, like XML, RTF, DOC, PDF... I have serched through many places and come up empty handed with
anything that works. /sad.gif' border='0' style='vertical-align:middle' alt='sad.gif' /> ....
Replace Shtml With Php?
I don't know jack.... (1) I don't know anything about PHP, except that I use phpBB and have modified my forums template
files. I used to make web sites years ago and just got back into it a couple months ago. I wonder
if I should stop using shtml and switch to php. I don't really have time to learn php (at least
not yet), but if it's easy to replace my Server Side Include with php (just to be more "modern")
I would be willing to do so. Anyone have any suggestions about me doing so? Am I'm too old
school for using shtml? Lol /laugh.gif' border='0' style='vertical-align:middle' ....
Looking for replace, character, specific, tag
|
|
Searching Video's for replace, character, specific, tag
|
advertisement
|
|