|
-
Aug 2nd, 2002, 12:09 PM
#1
Thread Starter
Hyperactive Member
Replce except text between
How can I replce string with another string except string between [E] and [/E]
for example: In This string
$Text="Hi how Are you [E] A A A [/E] Fine A A [E] A any text A [/E]";
I want to replce A with B but ignore A letter that between [E] and [/E]
How can i do that ?
Last edited by prokhaled; Aug 2nd, 2002 at 02:44 PM.
-
Aug 2nd, 2002, 01:22 PM
#2
Fanatic Member
Kinda like how vBulletin Code works?
PHP Code:
$text = "Hi how Are you [E] A A A [/E] Fine A A [E] A any text A [/E]";
$text = preg_replace("/\\[E\\]([A])\\[\\/E\\]","B",$text);
echo $text;
I am not sure if that is what you want, as your question is not very clear. Can you provide an example of how th output should look like?
-
Aug 2nd, 2002, 02:48 PM
#3
Thread Starter
Hyperactive Member
OUTPUT
$Text will be
$Text="Hi how Bre you [E] A A A [/E] Fine B B [E] A any text A [/E]";
that mean that all A string between [E] and [/E] will not change.
-
Aug 2nd, 2002, 03:12 PM
#4
Fanatic Member
It is easier replace the A in the [E] [/E] than outside of them. If I new more preg/reg expressions I would attempt it, but I am pretty novice at them. 
-Matt
-
Aug 2nd, 2002, 03:39 PM
#5
Thread Starter
Hyperactive Member
any way
I try your code .. but it give me this error :
Warning: No ending delimiter '/' found in C:\apache\htdocs\r.php on line 4
-
Aug 2nd, 2002, 03:50 PM
#6
Stuck in the 80s
I have an idea how to do it, but I have to go to work. If no one's helped you by then, I'll give it a shot when I get home (around 12:00 EST)
-
Aug 2nd, 2002, 05:51 PM
#7
Frenzied Member
PHP Code:
<?php
$text1 = "Hi how Are you [E] A A A [/E] Fine A A [E] A any text A [/E]";
if (preg_match('/(.*) (\\[E\\])(.*)(\\[\\/E]) (.*) (\\[E\\])(.*)(\\[\\/E])/', $text1, $match)){
$rep = str_replace("A", "B", $match[5]);
$done = $match[1].$match[2].$match[3].$match[4].$rep.$match[6].$match[7].$match[8];
echo "<p>".$done;
}else{
echo "doesn't match";
}
?>
Although I don't see the purpose of this.
-
Aug 2nd, 2002, 10:46 PM
#8
Stuck in the 80s
By looking at your code, it will only work with the example text he gave, won't it?
I think he needs to to work no matter what the text is...
-
Aug 2nd, 2002, 11:16 PM
#9
Stuck in the 80s
This is what I came up with:
PHP Code:
<?php
$test1 = "Hi how Are you [E] A A A [/E] Fine A A [E] A any text A [/E]";
echo "Test1 = " . $test1 . "<br>After Function, = " . convert($test1) . "<br><br>";
//THE FUNCTION:
function convert($text) {
if (!strpos($text, "[E]") === false) {
do {
$pos = strpos($text, "[E]", $epos);
$ntext .= str_replace("A", "B", substr($text, $epos, $pos - $epos)); // . "<br>";
$epos = strpos($text, "[/E]", $pos) + 4;
$ntext .= substr($text, $pos, $epos - $pos);
} while (!strpos($text, "[E]", $epos) === false);
} else {
$ntext = str_replace("A", "B", $text);
}
return $ntext;
}
?>
-
Aug 3rd, 2002, 03:44 AM
#10
Thread Starter
Hyperactive Member
Thank you
But i have another good idea
How can I replce A letter that between [E] and [/E]
I try the code of cpradio:
$text = preg_replace("/\[E\]([A])\[\/E\]","B",$text);
but it give me error.
the idea to replace A letter between [E] and [/E] with any strange string for example (!*!*!) and after that replace all A string with B string and after that replace strange string (!*!*!) with A letter
So what is the error in this code:
$text = preg_replace("/\[E\]([A])\[\/E\]","B",$text);
-
Aug 3rd, 2002, 10:28 AM
#11
Frenzied Member
you are correct Hobo, that will only work with what string he had here.
prokhaled, Matt's code is missing a /
$text = preg_replace("/\[E\]([A])\[\/E\]/","B",$text);
-
Aug 3rd, 2002, 11:10 AM
#12
Thread Starter
Hyperactive Member
no effect at all
I try that
<?
$text = "Hi how Are you [E] A A A [/E] Fine A A [E] A any text A [/E]";
$text = preg_replace("/\[E\]([A])\[\/E\]/","B",$text);
Echo $text;
?>
but no thing changed it's echo the same string without replace any thing
output:
Hi how Are you [E] A A A [/E] Fine A A [E] A any text A [/E]
-
Aug 3rd, 2002, 11:38 AM
#13
Stuck in the 80s
Re: Thank you
Originally posted by prokhaled
But i have another good idea
Actually, it was Matt's idea...
So now you want the complete opposite?
-
Aug 3rd, 2002, 11:43 AM
#14
Thread Starter
Hyperactive Member
-
Aug 3rd, 2002, 11:48 AM
#15
Stuck in the 80s
Re: yes opposite
Originally posted by prokhaled
opposite but short code.
so read through the manual and find out how to do something on your own for once.
-
Aug 3rd, 2002, 11:56 AM
#16
Frenzied Member
Re: Re: yes opposite
Originally posted by The Hobo
so read through the manual and find out how to do something on your own for once.
now you know why I did it one way. why waste my time when he changes his mind so much and never does anything on his own.
$text1 = "Hi how Are you [E] A A A [/E] Fine A A [E] A any text A [/E]";
$text = preg_replace("/(A) /"," B ",$text1);
echo $text;
-
Aug 3rd, 2002, 12:10 PM
#17
Stuck in the 80s
He's already asked an almost similiar question before...
http://www.vbforums.com/showthread.p...hreadid=184356
-
Aug 3rd, 2002, 12:40 PM
#18
Fanatic Member
Personally, my code does not work b/c I did not put time into checking it. I just wrote it as an example to go from.
If you do a search on many popular forums you will figure out how preg and reg expressions work. It isn't complicated it just takes time and patience.
Learning is not a bad thing to do. Depending on others however is.
-
Aug 3rd, 2002, 03:58 PM
#19
Thread Starter
Hyperactive Member
Thanks for all
I try it
$text1 = "Hi how Are you [E] A A A [/E] Fine A A [E] A any text A [/E]";
$text = preg_replace("/(A) /"," B ",$text1);
echo $text;
output was:
Hi how Are you [E] B B B [/E] Fine B B [E] B any text B [/E]
but it must be:
Hi how Are you [E] B B B [/E] Fine A A [E] B any text B [/E]
-
Aug 3rd, 2002, 08:06 PM
#20
Frenzied Member
yup that is what I coded it for. now you have the means you can change it so it will change what you want.
-
Aug 3rd, 2002, 10:09 PM
#21
Stuck in the 80s
Re: Thanks for all
Originally posted by prokhaled
I try it
$text1 = "Hi how Are you [E] A A A [/E] Fine A A [E] A any text A [/E]";
$text = preg_replace("/(A) /"," B ",$text1);
echo $text;
output was:
Hi how Are you [E] B B B [/E] Fine B B [E] B any text B [/E]
but it must be:
Hi how Are you [E] B B B [/E] Fine A A [E] B any text B [/E]
Prokky, I think he's telling you to do it on your own.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|