Archive for February, 2009

Php: (strpos) Search for text in a string

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:

<br />
$string = &#8216;thedog&#8217;;<br />
var_dump(strpos($string,&#8217;dog&#8217;));<br />
Output: int(3)<br />

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

<br />
$string = &#8216;thecat&#8217;;<br />
var_dump(strpos($string,&#8217;dog&#8217;));<br />
Output: bool(false)<br />

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:

<br />
$string = &#8216;thecat&#8217;;<br />
if(strpos($string,&#8217;the&#8217;)){<br />
    echo &#8216;The is in the string&#8217;;<br />
}else{<br />
    echo &#8216;The is not in the string&#8217;;<br />
}<br />
Output: The is not in the string<br />

What?? Lets take a closer look:
<br />
$string = &#8216;thecat&#8217;;<br />
var_dump(strpos($string,&#8217;the&#8217;));<br />
Output: int(0)<br />

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:

<br />
$string = &#8216;themonkey&#8217;;<br />
if(strpos($string,&#8217;monkey&#8217;) !== false){<br />
    echo &#8216;There is a monkey in my string&#8217;;<br />
}<br />
Output: There is a monkey in my string<br />

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

Tags:

Saturday, February 28th, 2009 Programming, php No Comments

Parsing a CSV file with ASP

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

Tags:

Thursday, February 12th, 2009 Programming No Comments

Using jQuery to rewrite all relative url’s

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:
› Continue reading

Tags: ,

Wednesday, February 11th, 2009 JavaScript, Programming No Comments

Powered by Twitter Tools