Tag Archives: php

How to calculate number of lines in PHP

Tweet If you search for this topic in a search engine, most of them will tell you that the best way is: count(explode(“\n”, $line)); But it’s not the most efficient. It’s actually substr_count($line, “\n”); On my workstation, running this 500,000 times on a line of 380 lines shows that substr_count executes in 19 seconds whereas [...]

Weird characters when parsing CSV in PHP

Tweet For some reason, if you do this PHP $csv_file = file_get_contents(site_url($file_name)); $csv_lines = explode(“n”, $csv_file); a CSV line like this Campaign Name    Ad Group Name    Component Type    … turns into a weird line like this: 377376C^@a^@m^@p^@a^@i^@g^@n ^@N… This has to do with PHP’s character encoding issues. Here’s the easier way to take care of [...]

How to search for html TITLE tag with PHP

Tweet I know this is probably a lame post, but i’ll do it anyway. If you want to search for HTML title, you’ll notice everyone gives out an answer like this: $matches = array(); $pattern = ‘/<title>(.*)<\/title>/’; preg_match($pattern, $html, $matches); $pagetitle = $matches[1]; But this actually doesn’t work in cases where there’s extra newline character. [...]