![]() |
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"; Code:
Best niet [b] bevet Moddervet Niet vet |
I did work a bit with regular expressions, but not in PHP.
Can you first explain what do you like program to do? |
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. |
This is PHP? :blink:
|
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.
|
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. |
Var_dump() gives wrong results too.
Code:
$string = "kakakapipikaka"; 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 :) |
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 |
So eregi() stops the moment it finds a match. That's pretty useful, but not for what I'm trying to do :D Thanks for the clarification :cheers:
|
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 |
The current time is 11:56 PM (GMT) |
Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.