Identify how many coma are there

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
winsonlee
Forum Commoner
Posts: 76
Joined: Thu Dec 11, 2003 8:49 pm

Identify how many coma are there

Post by winsonlee »

i wonder if there is a function to count how many coma are there in a string ??

vb has function such as instr, left and right. Are there any in php ???
how can i split a string eg

"today is sunny*today is raining^today is cloudy"

into

"today i sunny"
"today is raining"
"today i cloudy"

help is greatly appreciated. thanks.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

well with that string you could do

Code: Select all

$first = explode('*', $string);
$second = explode('^', $first[1]);
$arr[] = $first[0];
$arr[] = $second[0];
$arr[] = $second[1];

print_r($arr);
but if its just commas that would be a whole lot easier
thegreatone2176
Forum Contributor
Posts: 102
Joined: Sun Jul 11, 2004 1:27 pm

Post by thegreatone2176 »

yea if it was all commas you could just explode on , and then count()
winsonlee
Forum Commoner
Posts: 76
Joined: Thu Dec 11, 2003 8:49 pm

Post by winsonlee »

Code: Select all

header(11/02/2004,4.00,Carpark).

present(bish tan,sarene tan,carlo pun).

absent(rezoto ng,jackie tan).

apology(willy chan, carment ng).

% discussion


point(1, [
	title = text("point i"),
	para = "abcdefghijakaka",
	action = "asfasfasf"
	para = "abcdefghijakddaka",
	para = "abcdefghadfijakaka",
	action = "asfasfafesf"
	]).

point(2, [
	title = text("point v"),
	para = "abcdefghijdaaaakaka",
	para = "abcdefghadfijakaka",
	action = "asfasfafesf"
	]).

footer(6.00).
all the following information will be stored in a text file. what i wanted is to extract the information out from the text file and display in a normal viewable html format.

eg


Date : 11/02/2004
Time : 4.00
Venue : Carpark

Present :
1) bish tan
2) sarene tan
3) carlo pun
...

1) point i

abcdefghijakaka
ACTION : asfasfasf

abcdefghijakddaka

abcdefghadfijakaka
ACTION : asfasfafesf
...
..



i know bout using explode. Just wondering if there are any other function that i can use in PHP to extract the information out other then explode ??
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

preg_match() and preg_match_all()... or a stack parser
winsonlee
Forum Commoner
Posts: 76
Joined: Thu Dec 11, 2003 8:49 pm

Post by winsonlee »

just wondering whats wrong with my regular expression ??
coz my preg match doesnt seems to be working

point([title = text("point i"),para1 = "abcdefghijakaka",action1 = "asfasfasf"]).
point([title = text("point v"),para1 = "abcdefgddahijakaka",action1 = "asfasdffasf"]).

i wan the following line to be printed as

title = text("point i"),para1 = "abcdefghijakaka",action1 = "asfasfasf"
title = text("point v"),para1 = "abcdefgddahijakaka",action1 = "asfasdffasf"

i tried using

preg_match_all("point([(.*)]).",
$body,
$out, PREG_PATTERN_ORDER);
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

preg_match_all('#point\(\[(.*?)\]\)\.#s',$body,$out);
print_r($out);
winsonlee
Forum Commoner
Posts: 76
Joined: Thu Dec 11, 2003 8:49 pm

Post by winsonlee »

foreach ( $out[1] as $valuex ) {
echo $valuex;

}

i tried using the following code to print out the following. But it doesnt seems to work.Anything wrong with the code ??


title = text("point i"),para1 = "abcdefghijakaka",action1 = "asfasfasf"

title = text("point v"),para1 = "abcdefgddahijakaka",action1 = "asfasdffasf"
winsonlee
Forum Commoner
Posts: 76
Joined: Thu Dec 11, 2003 8:49 pm

Post by winsonlee »

sorry .. it works . i got it mixed up
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

mistakenly posted to a new thread:
winsonlee wrote:preg_match_all('#point\(\[(.*?)\]\)\.#s',$body,$out);


some how the regular expression above doesnt allow the coma to get through. any idea how i can solved this problem ??

allow the commas to get through? the commas contained in the text found inside the point() is captured, I'm not sure what your problem is really.
Post Reply