26-01-2006, 09:32 AM | #1 | ||
Join Date: Sep 2004
Location: Dentergem, Belgium
Posts: 1,811
|
I'm learning Regex now, and'm quite stumped. I'd ask the instructor, but he's said to be no good with them either yesterday. I've made a test-page to get the hang of it, but its output's not what it should do, according to how I read the code.
Code:
*$string = "Best niet [b] bevet Moddervet Niet vet"; *echo $string, "<br>"; *eregi("(\[b\])", $string, $resultaat); *$open_tags = count($resultaat); *eregi("(\[/b\])", $string, $resultaat_closed); *$closed_tags = count($resultaat_closed); *echo "Open: ", $open_tags, "<br>Closed: ", $closed_tags; Code:
Best niet [b] bevet Moddervet Niet vet Open: 2 Closed: 0 |
||
|
|
26-01-2006, 09:59 AM | #2 | ||
Join Date: Nov 2004
Location: Afrim, Albania
Posts: 2,113
|
I did work a bit with regular expressions, but not in PHP.
Can you first explain what do you like program to do? |
||
|
|
26-01-2006, 10:32 AM | #3 | ||
Join Date: Sep 2004
Location: Dentergem, Belgium
Posts: 1,811
|
I would like the program to tell me how many times it encounters [*b*] and how many times it encounters [/*b*] (without the *s) in a certain string. It then should add as many [/*b*]s as needed if the amount of [*b*]s is bigger than the amount of [/*b*]s.
In the end, it should turn all the [*b*][/*b*]-tags to HTML's [b] tags, but it should close as many as are opened, or it'd mess up my input. I eventually'd like to do that with hyperlinks too, which makes it so complicated. |
||
|
|
26-01-2006, 10:59 AM | #4 | ||
Join Date: Jul 2005
Posts: 1,127
|
This is PHP? :blink:
|
||
|
|
26-01-2006, 11:23 AM | #5 | ||
Join Date: Sep 2004
Location: Dentergem, Belgium
Posts: 1,811
|
Yep, regular expressions within PHP. It's one of the biggest brainwreckers out there for it, but sometimes a necessity. For more basic information, you can always look here. The site explains what it is, but it won't help me solve problem. Preg_match_all() can, though. Am going to try that one out now.
|
||
|
|
26-01-2006, 11:29 AM | #6 | ||
Join Date: Jun 2004
Location: Jan Mayen, Svalbard and Jan Mayen
Posts: 2,167
|
uhmn you misunderstood what is placed in $resultaat
in $resultaat[0] the copy of the total matched string is placed in $resultaat[1] is the match for the first () sequence in $resultaat[2] is the match for the second () sequence stored. so if you match for [*b*] then you will find in 0 [*b*] (as it's the full match) and in 1 (as it the first set of () that is matched). As you don't match for a second pair of () you will find any more matches. The count idea you are trying to implement will not work this way.
__________________
Flowing with the stream of life |
||
|
|
26-01-2006, 11:42 AM | #7 | ||
Join Date: Sep 2004
Location: Dentergem, Belgium
Posts: 1,811
|
Var_dump() gives wrong results too.
Code:
$string = "kakakapipikaka"; $reg_ex = "(ka)"; eregi($reg_ex, $string, $test); echo "Test: ", var_dump($test), "<br>"; Code:
Test: array(2) { [0]=> *string(2) "ka" [1]=> *string(2) "ka" } A different solution works now, though. $number = preg_match_all($reg_ex, $string, $resultaat) returns 5 |
||
|
|
26-01-2006, 11:52 AM | #8 | ||
Join Date: Jun 2004
Location: Jan Mayen, Svalbard and Jan Mayen
Posts: 2,167
|
well the example you posted using var_dump gives the correct output for
ereg(i) you don't understand what eregi matches. (it only matches the string ka once.) and as that is the total string as well you see it in both 0 and 1
__________________
Flowing with the stream of life |
||
|
|
26-01-2006, 11:56 AM | #9 | ||
Join Date: Sep 2004
Location: Dentergem, Belgium
Posts: 1,811
|
So eregi() stops the moment it finds a match. That's pretty useful, but not for what I'm trying to do Thanks for the clarification
|
||
|
|
26-01-2006, 12:17 PM | #10 | ||
Join Date: Jun 2004
Location: Jan Mayen, Svalbard and Jan Mayen
Posts: 2,167
|
well it can match it more often if you specify it in the regexstring.
but it's not suitable for counting. the preg_match_all seems a better choice for that
__________________
Flowing with the stream of life |
||
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
regular | sgtboat | Blah, blah, blah... | 4 | 26-11-2008 03:30 AM |
When will regular updates start? | Doink | Blah, blah, blah... | 6 | 13-12-2007 10:00 AM |
|
|
||
  |