Go Back   Forums > Community Chatterbox > Tech Corner > Programming
Memberlist Forum Rules Search Today's Posts Mark Forums Read
Search Forums:
Click here to use Advanced Search

Reply
 
Thread Tools Display Modes
Old 26-01-2006, 09:32 AM   #1
Kon-Tiki
[BANNED]

 
Join Date: Sep 2004
Location: Dentergem, Belgium
Posts: 1,811
Default

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;
That's my code (stripped of all set-up tags like <html> etc). This's its output:
Code:
Best niet [b] bevet Moddervet Niet vet
Open: 2
Closed: 0
No matter how many results it should find, it'll always give 2, except if there're no results, like in Closed. Then it'll give 0. I don't see why it doesn't give 1 for the Open one in the current example. Any help'd be greatly appreciated
Kon-Tiki is offline                         Send a private message to Kon-Tiki
Reply With Quote
Old 26-01-2006, 09:59 AM   #2
Rogue
10 GOSUB Abandonia
20 GOTO 10
 
Rogue's Avatar

 
Join Date: Nov 2004
Location: Afrim, Albania
Posts: 2,113
Default

I did work a bit with regular expressions, but not in PHP.

Can you first explain what do you like program to do?
Rogue is offline                         Send a private message to Rogue
Reply With Quote
Old 26-01-2006, 10:32 AM   #3
Kon-Tiki
[BANNED]

 
Join Date: Sep 2004
Location: Dentergem, Belgium
Posts: 1,811
Default

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.
Kon-Tiki is offline                         Send a private message to Kon-Tiki
Reply With Quote
Old 26-01-2006, 10:59 AM   #4
Bobbin Threadbare
Not quite dead.
 
Bobbin Threadbare's Avatar

 
Join Date: Jul 2005
Posts: 1,127
Unhappy

This is PHP? :blink:
__________________
Bobbin Threadbare is offline                         Send a private message to Bobbin Threadbare
Reply With Quote
Old 26-01-2006, 11:23 AM   #5
Kon-Tiki
[BANNED]

 
Join Date: Sep 2004
Location: Dentergem, Belgium
Posts: 1,811
Default

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.
Kon-Tiki is offline                         Send a private message to Kon-Tiki
Reply With Quote
Old 26-01-2006, 11:29 AM   #6
Data
retired
 
Data's Avatar


 
Join Date: Jun 2004
Location: Jan Mayen, Svalbard and Jan Mayen
Posts: 2,167
Default

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
Data is offline                         Send a private message to Data
Reply With Quote
Old 26-01-2006, 11:42 AM   #7
Kon-Tiki
[BANNED]

 
Join Date: Sep 2004
Location: Dentergem, Belgium
Posts: 1,811
Default

Var_dump() gives wrong results too.
Code:
$string = "kakakapipikaka";
$reg_ex = "(ka)";
eregi($reg_ex, $string, $test);
echo "Test: ", var_dump($test), "<br>";
Output:
Code:
Test: array(2) { [0]=> *string(2) "ka" [1]=> *string(2) "ka" }
The way that should be, would be [0]=> string(2) "ka" [1]=> string(2) "ka" [2]=> string(2) "ka" [3]=> string(2) "ka" [4]=> string(2) "ka" [5]=> string(2) "ka" (with something different for [0]), if I understand correctly.

A different solution works now, though.
$number = preg_match_all($reg_ex, $string, $resultaat) returns 5
Kon-Tiki is offline                         Send a private message to Kon-Tiki
Reply With Quote
Old 26-01-2006, 11:52 AM   #8
Data
retired
 
Data's Avatar


 
Join Date: Jun 2004
Location: Jan Mayen, Svalbard and Jan Mayen
Posts: 2,167
Default

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
Data is offline                         Send a private message to Data
Reply With Quote
Old 26-01-2006, 11:56 AM   #9
Kon-Tiki
[BANNED]

 
Join Date: Sep 2004
Location: Dentergem, Belgium
Posts: 1,811
Default

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
Kon-Tiki is offline                         Send a private message to Kon-Tiki
Reply With Quote
Old 26-01-2006, 12:17 PM   #10
Data
retired
 
Data's Avatar


 
Join Date: Jun 2004
Location: Jan Mayen, Svalbard and Jan Mayen
Posts: 2,167
Default

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
Data is offline                         Send a private message to Data
Reply With Quote
Reply


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

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump
 


The current time is 11:13 AM (GMT)

 
Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.