Is it pasible to find all occurrences of strings "North" and "North-East" in some txt file and save number of occurrences in some variable??

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

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

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

Won't line 5 echo '4', instead of '3' ? (Don't think I'm nagging, just keeping you on your toes :) )

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"

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

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

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 ;

Thank you kind sir:D everything work perfectly.

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

Member Avatar for diafol

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.

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

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.

Member Avatar for diafol

@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);

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 ;)

commented: thanks for the explanation +14
Member Avatar for diafol

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

Thanks for help. Thread is ready to be closed :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.