4
Contributors
17
Replies
25
Views
5 Years
Discussion Span
Last Post by Drugsxxx
Featured Replies
  • kept thing simple first get content of file through "file_get_contents" like {$text = file_get_contents("./folder/file.txt", true);} now you have array of file content in $text variable now use {echo substr_count($my_text ,"North"); } this will print number of occurrence of substring in file. Hopefully this will work for you. if yes please … Read More

  • Why don't you subtract "North-East" from "North" You get the total number [CODE] $getText = file_get_contents("text.txt", true); $North = substr_count($getText ,"North"); $North_East = substr_count($getText ,"North-East"); $getNorth = $North - $North_East ; echo "North =". $getNorth; echo "<br>"; echo "North_East =". $North_East ; [/CODE] Read More

  • 1

    Agree on the latter. In the first, the [iCODE]\b[/iCODE] within the brackets means backspace, and the pipe does not mean 'or' (just the pipe char), so it matches [iCODE]'North '[/iCODE] twice because of the space. Try it with North at the end of the string ;) Read More

  • 1
    diafol 3,669   5 Years Ago

    @P - thanks for the explanation - it looked all wrong - so decided on the array_count_values solution. Read More

1

kept thing simple

first get content of file through "file_get_contents"

like {$text = file_get_contents("./folder/file.txt", true);}

now you have array of file content in $text variable

now use {echo substr_count($my_text ,"North"); }

this will print number of occurrence of substring in file.

Hopefully this will work for you.

if yes please mark this as completed with positive remarks

thanks

0

Thx for your answer kind sir.I must specify what i mean.

I have a .txt file and data in it looks like this:

12.10.2011 19.58,North
12.10.2011 20,00,North
12.10.2011 20.02,North
12.10.2011 2010,North-East

and i wish to chek the file for occurrences and write them in variable somthing like this:

$North = 3
$North-East = 1

0

Well i think you need complete code for this

$getText = file_get_contents("text.txt", true);
  $North = substr_count($getText ,"North");
  $North_East = substr_count($getText ,"North-East");
  
	echo "North =".  $North; 
	echo "<br>";
	echo "North_East =".  $North_East ;

This is working example same as you write txt file data

0

Yes this is working fine for me. Thx for this.

But $North will have count north double - for "North" and for "North-East". What should i do for corect result. I mean #north count without "North-East" only "North"

-1
$getText = file_get_contents("text.txt", true);
$North = substr_count($getText ,"North");
  echo "North =".  $North;
0

Yes yes this is corect but i was thinking about somthing else.

with your code:

$getText = file_get_contents("text.txt", true);
  $North = substr_count($getText ,"North");
  $North_East = substr_count($getText ,"North-East");
 
	echo "North =".  $North; 
	echo "<br>";
	echo "North_East =".  $North_East ;

and .txt file loking like this:

North
North
North
North-East
North-East
North-East

echos will give somthing lie this:
North =6 // and should be 3 but North-East have north string too how to do corect result??
North_East =3

1

Why don't you subtract "North-East" from "North" You get the total number

$getText = file_get_contents("text.txt", true);
$North = substr_count($getText ,"North");
$North_East = substr_count($getText ,"North-East");
$getNorth = $North - $North_East ;
echo "North =".  $getNorth; 
echo "<br>";
echo "North_East =".  $North_East ;
0

That's good, please mark this thread as solved, and if this is helpful please remark me positively
regards

0

This may be a little unsafe. preg_match?

preg_match_all("/\bNorth[ |\b]/",$str,$matches);
echo count($matches);

preg_match_all("/\bNorth-East\b/",$str,$matches);
echo count($matches);

preg_match is a little slower than substr_count if think, but more versatile.

0

@ardav: Are you sure that first one does it, because I think a - is also a \b

Edited by pritaeas: n/a

0

Why don't you subtract "North-East" from "North" You get the total number

That works in this particular case only, how about:

North
North-West
North Cape
Cape North-East
Cape North-West
Cape North

You never know what kind of data you are getting. Such assumptions are dangerous.

Edited by pritaeas: n/a

0

@Pritaeas

$str = "North By North-East By North-East by North by South By North-East";

preg_match_all("/\bNorth[ |\b]/",$str,$matches);
print_r($matches[0]);

gives me 2 x North - so I assume it works.

It was a quick trial, so I'm sure a better pattern exists.

However, thinking about it, this?

$str = "North By North-East By North-East by North West by South By North-East";
preg_match_all("/(North[^-]|North-East|South[^-]|South-East|North-West|South-West|West|East)/",$str,$matches);
$arr = array_count_values($matches[0]);
print_r($arr);

Edited by diafol: n/a

1

Agree on the latter.

In the first, the \b within the brackets means backspace, and the pipe does not mean 'or' (just the pipe char), so it matches 'North ' twice because of the space. Try it with North at the end of the string ;)

Edited by pritaeas: n/a

Votes + Comments
thanks for the explanation
1

@P - thanks for the explanation - it looked all wrong - so decided on the array_count_values solution.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.