Php: (strpos) Search for text in a string

0

If you want to search for text inside a string but don’t need fancyness of regex, here lies strpos.

I see a lot of posts online about people confused how strpos works and how to use it for searching for text in a string properly. First thing to note is that strpos() returns the index of the location of the string (not true if the string exists in part of the testing string). So:

$string = 'thedog';
var_dump(strpos($string,'dog'));
Output: int(3)

How does this benefit you if you want to make sure that $string doesn’t contain dog:

$string = 'thecat';
var_dump(strpos($string,'dog'));
Output: bool(false)

As you can see the result was false, but sticking that strpos straight inside your if statement is bad and erroneous code… Here’s why:

$string = 'thecat';
if(strpos($string,'the')){
    echo 'The is in the string';
}else{
    echo 'The is not in the string';
}
Output: The is not in the string

What?? Lets take a closer look:

$string = 'thecat';
var_dump(strpos($string,'the'));
Output: int(0)

So you see, the reason the if statement failed is because the if statement fails on a value of 0.

The proper way to test with strpos is as follows:

$string = 'themonkey';
if(strpos($string,'monkey') !== false){
    echo 'There is a monkey in my string';
}
Output: There is a monkey in my string

strpos() will return false if monkey is not in $string so the sure fire way to test to make sure it isn’t in there is to say that the return value is explictly not false. !== confuse you? This tests for an actual boolean value of false. Other values make cause an if statement to fail (such as 0 mentioned above)

(Read more about type comparison here. http://us2.php.net/manual/en/types.comparisons.php)

If data types are making you say huh?? See here: http://us3.php.net/manual/en/language.types.php, I will post later briefly about php and data types, if you have used Java than you live and die by data types, but php has a very simple concept when it comes to data types which makes coding in general easy but relying on and full understanding your own code sometimes difficult.

-Ak

Parsing a CSV file with ASP

0

I’m no fan of ASP especially after having to work with it… but regardless I had to.

Problem:

I don’t want to try and get a database running for a simple task that would require one table with few rows, but I also didn’t want to jump in and edit html when one small change had to take place.

Solution:

Build a csv file and use asp to parse it.

It still doesn’t make much sense to me but what it does basically pushes all the data into one giant line like (a1,b1,c1,d1,a2,b2,c2,d2) and it counts the number of columns and rows you have. The for loop then uses these number to put it into a multi-dimensional array based off of (row,column)
Here is the code:

csv_to_read="csvfile.csv"
set fso = createobject("scripting.filesystemobject")
set act = fso.opentextfile(server.mappath(csv_to_read))
imported_text = act.readline
imported_text = replace(imported_text,chr(13),",")
imported_text = replace(imported_text,chr(34),"")
split_text=split(imported_text,",")
num_imported=ubound(split_text)+1
total_imported_text = act.readall
total_imported_text = replace(total_imported_text,chr(13),",")
total_imported_text = replace(total_imported_text,chr(34),"")
total_split_text=split(total_imported_text,",")
total_num_imported=ubound(total_split_text)
trows = (total_num_imported/ (num_imported)-1)
'Have to pre-initialize the size of the array
Dim array(10,25)
count=0
for column = 0 to trows
	for row = 0 to num_imported -1
	array(row,column) = total_split_text(count)
	count=count+1
	next
Next

To pull a single value out of that (passed via url like file.asp?z=7) I do the following:

input=CInt(Request.QueryString("z"))
'Fill up the variables I need until they are what was actually passed in url and exit
For i = 0 to trows
	id = CInt(Replace(array(0,i),chr(10),""))
	title = array(1,i)
	price = array(2,i)
	If id = input Then
		Exit For
	End If
Next

'Now I go on my merry way and use the variables as needed:
    <h1><% Response.Write(title) %></h1>
    <h2><% Response.Write(price) %></h2>

Conclusion

This wasn’t quick to figure out but it is dirty and I only wrote it because I have 0 asp knowledge and did not want to try and learn how to connect asp to a MySQL or SQL Server at GoDaddy (not worth the trouble, and gladly I’ll be moving this site to my own server soon enough)

One thing I can’t argue with in a situation like this is if it’s not broken, don’t fix it!

Another intersted bit of code I had to figure out to do in asp was to always print the last day of the current month. I will post that code soon.

-Ak

Using jQuery to rewrite all relative url’s

0

This post has been revisited, see the newer version here.

Problem:

I have a template file that has all relative url’s for all the links and I have this template on two different subdomains, one is used for processing and the other is for static files.
When I display something on my processing server I want it to make all the links point the the static pages without modifying the template.

Solution:

Use jQuery to parse all links and change to the proper domain.

Question:

Is there a better method that is automatic such as this? Or maybe a better way to write the jQuery as it stumped me to get it right for a bit.

Code:

Read more

Domain Name Search Engine Registration Mail Scam

1

Believe it or not scams even come through snail mail. This has been around for a while but it is the first time I’ve seen it personally. The overall goal of this scam is to make you think that your domain name is expiring and to hurry and send them money before it expires.

Here is the main body of the letter:

As a courtesy reminder, we are sending you this notification for you business Domain name search engine registration. This letter is to inform you that it’s time to send in your registration and save.

You must complete your domain name search engine registration to guarantee your listing on the Web, and now is the time to register and save.

Failure to complete your Domain name search engine registration by the expiration date may result in cancellation of this offer making it difficult for customers to locate you on the web.

Privatization allows for the consumer a choice when registering. Search engine subscription includes domain name search engine submission. You are under no obligation to pay the amounts stated below unless you accept this offer. Do not discard; this notice is not an invoice it is a courtesy reminder to register your domain name search engine listing so your customers can locate you on the web.

This notice for: WWW.DOMAIN.COM will expire on 02/10/2009 Act Today!

Read more

Hosting Performance

0

So as I was making some posts and doing some backend work within wordpress I noticed some really slow requests… If you browse my blog what do you think about the performance? I hadn’t had complaints before and I wonder if something is going on right now (shared hosting…)

Amidst the fray I’m thinking more and more about hosting myself. I’ve migrated one of my clients partially to this host so I could use php 5+ but am reluctant to move them completely here if the performance isn’t really there.

I like the prices available @ slicehost. It will require me to secure & me to monitor etc etc but it is probably worth the performance increase and unshared hosting.

Creating JSON as a select result in a MySQL Query

1

So I had this crazy idea at work, I needed to get all the data out of an entire table in a single column. So I decided to return the table as a preformatted JSON array that I could decode straight into a php array for manipulation.

The Code:

Read more

The theme again.

0

I ended up finding a really nice theme that was still inside my desired color scheme yet was very clean and easy on the eyes. Theme is called deCoder. Here’s a link to the developer’s website. http://webdemar.com/