Jump to content

match once


glenelkins

Recommended Posts

hi

 

i maybe have a string like "hellohellobyehello"

 

i want to match for "hello" so use:

 

preg_match ( '%hello%', $string );

 

but how would i return true if the string contains only 1 occurance of "hello". so in this example it would be false because there are 3 "hello" in there

Link to comment
https://forums.phpfreaks.com/topic/178193-match-once/
Share on other sites

ok all well and good but the string and pattern will have more than just in my example to macth.

 

it may have a string like: '1234hellobyedonttalktome'

 

pattern may be '%1234hello%';

 

only want to make sure the string has 1 occurance of the "hello" but must start with 1234

Link to comment
https://forums.phpfreaks.com/topic/178193-match-once/#findComment-939517
Share on other sites

If you need to match multiple things you might have wanted to mention that initially. With regular expressions, theres no point in saying I want to match this, getting a pattern then saying oh it must also match this... the chances are the pattern will be changed completely. It's important to know from the outset exactly what needs to be matched. Having said that, in your particular example you're probably better off using multiple patterns. Also using the information provided so far your probably (IMHO) better off using str functions rather than regex.

 

if(substr($input, 0, 4) == '1234' && substr_count($input, 'hello') == 1) {

Link to comment
https://forums.phpfreaks.com/topic/178193-match-once/#findComment-939571
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.