473,320 Members | 2,158 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Is there an operator to compare likeness in PHP?

Markus
6,050 Expert 4TB
As you see in MySQL there is the "LIKE ='%something%'"

I was wondering if there was a way to do this within PHP?

Thanks :)
Nov 5 '07 #1
11 1512
code green
1,726 Expert 1GB
The closest is strstr() which searches for a string within a string
Nov 5 '07 #2
Motoma
3,237 Expert 2GB
The closest is strstr() which searches for a string within a string
I think a much nicer function to try is preg_match()! The only caveat is that you will need to use regular expressions.
Nov 5 '07 #3
tj111
12
Don't use regular expressions unless you need them. Regular expression take longer to run and use up more memory. I just replied to a convo about this earlier today, you can see my test results <LINK REMOVED>
Nov 5 '07 #4
Motoma
3,237 Expert 2GB
Don't use regular expressions unless you need them. Regular expression take longer to run and use up more memory. I just replied to a convo about this earlier today, you can see my test results <LINK REMOVED>
Compare both of those results to the amount of time for the request, round trip from handshake until the acknowledgment of the final packet. Add the execution times into the transfer time, and then give a final estimate on the difference in speed. I would be surprised if it was more than 10% overhead.
Additionally, you could include a version of your test which did a search for "toasty%sub" and see if, after the logic for the two combined searches complete, there is a performance difference which worries you.
Nov 5 '07 #5
Markus
6,050 Expert 4TB
You make me feel soo self conscious about my progress with PHP.

I think my a-levels are going to have to reside in computing of some sort, so's i can catch up with you guys ¬_¬

Thanks for the replies and guidance!

:)
Nov 5 '07 #6
Markus
6,050 Expert 4TB
While we're here,

i see things like this ALL over the place, i.e. email validation and such.

[php]
'/^[a-z]*$/'
[/php]

Anyone care to elaborate on what this is and does?

:)
Nov 5 '07 #7
Motoma
3,237 Expert 2GB
While we're here,

i see things like this ALL over the place, i.e. email validation and such.

[php]
'/^[a-z]*$/'
[/php]

Anyone care to elaborate on what this is and does?

:)
The ^ indicates the start of a string, the $ indicates the end of one. [a-z] represents all lowercase letters, while * indicates that there are no limits on the number of them. Therefore, that string used in preg_match will tell you whether or not the string is made only of lowercase alphabet characters.

Note: were the pattern string to contain an i after the final slash, it would perform a case-insensitive search, therefore upper and lowercase letters allowed.
Nov 5 '07 #8
Markus
6,050 Expert 4TB
Ok, i'm starting to understand it :)

Thanks!
Nov 5 '07 #9
ak1dnar
1,584 Expert 1GB
tj111, I had to remove a link to another forum, from your post because posting such links, against our site rules. For further clarification please read the 6th point of "Things that are generally unacceptable" on FAQs. In future, It's better to discuss everything here at the same thread without pointing Other forum links. Still you can post links to good articles on other web sites, but NOT the forums links.
Thanks for your time.
Nov 6 '07 #10
tj111
12
Sorry about that, no harm intended, was just feeling to lazy to rewrite it myself lol.

Here are the test results that I ran locally, which as an above poster mentioned probably aren't 100% accurate in a live environment, but is good for comparing overhead. Also, note that on longer strings, regular expressions seem to be more efficient than string functions (after ~5000 characters).
[php]
<?php
$STR = "test";
print (strspn($STR,"abcdefghijklmnopqrstuvwxyz") == strlen($STR)) ? "success" : "fail";

//this script took 0.029 ms to execute
?>
<?php
$str = "test";
print (preg_match('/^[a-z]*$/', $str)) ? "success" : "fail";

//this script took 0.368 ms to execute
//thats more than a 10x decrease in performance
?> [/php]

Also, that syntax is known as regular expressions, it's for matching patterns in strings. It's daunting at first, but the tutorial I found does a really good job of explaining it. It takes about an hour or so to read through, and tends to be slightly repetitive, but really hammers the point across to exactly how regular expressions work.
Nov 6 '07 #11
code green
1,726 Expert 1GB
Also, note that on longer strings, regular expressions seem to be more efficient than string functions (after ~5000 characters).
This is interesting.
Can you supply a reference.
Nov 7 '07 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Peter Meier | last post by:
Hello everybody, Stroustrup says he prefer's to declare operators, which do not do anything on the class itself global. Does anybody know the reason for that? Any advantages/disadvantages? ...
5
by: Teddy | last post by:
Hello all consider the class Date declaretion below: class Date { public: Date(); Date(int year, int month, int day); Date(const string&); int getYear() const;
14
by: indigodfw | last post by:
Greetings from India I would like to know the rationale on allowing structs to be assigned (using = operator) and not allowing equality operator ( == operator) on them. The compiler when it...
12
by: cody | last post by:
Why can I overload operator== and operator!= separately having different implementations and additionally I can override equals() also having a different implementation. Why not forbid...
34
by: Chris | last post by:
Is there ever a reason to declare this as if(*this == rhs) as opposed to what I normally see if(this == &rhs) ?
9
by: marko | last post by:
/* code start */ int a = 0; /* expected evaluation and excution order with precedence in mind /* False(3) , True(1), False(2) */ if ( (a=1) == 0 || 0 != 1 && (a =2) == 1) putchar('T');...
10
by: MasterGaurav \(www.edujini-labs.com\) | last post by:
Hi, I have a, probably very basic, query on value-types. Why cannot I use the == operator directly for value-types (not primitive types like int, float, double etc)? Why do I need to either...
12
by: y-man | last post by:
Hi, I am creating a child class of the vector, which contains a couple of functions to make the work with carthesian coordinate vectors easier. To make things work more easily, I would like to...
4
by: Travis | last post by:
Is there a way to determine the percentage likeness two strings share? So that given an parameter string1 and a list of strings to search, I could display only those values that are say, a 75% or...
1
by: Stuart Brockman | last post by:
Hi, I don't quite get what is going on in this code example: --------------------------- #include <iostream> using namespace std; class Base{ public:
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.