Get title of website via link

My answer is expanding on @AI W’s answer of using the title of the page. Below is the code to accomplish what he said. <?php function get_title($url){ $str = file_get_contents($url); if(strlen($str)>0){ $str = trim(preg_replace(‘/\s+/’, ‘ ‘, $str)); // supports line breaks inside <title> preg_match(“/\<title\>(.*)\<\/title\>/i”,$str,$title); // ignore case return $title[1]; } } //Example: echo get_title(“http://www.washingtontimes.com/”); ?> … Read more

Call to undefined function apache_request_headers()

You can use the following replacement function: <?php if( !function_exists(‘apache_request_headers’) ) { /// function apache_request_headers() { $arh = array(); $rx_http = ‘/\AHTTP_/’; foreach($_SERVER as $key => $val) { if( preg_match($rx_http, $key) ) { $arh_key = preg_replace($rx_http, ”, $key); $rx_matches = array(); // do some nasty string manipulations to restore the original letter case // this … Read more

split at the first space in a string

Use explode with a limit: $array = explode(‘ ‘, $string, 2); Just a side note: the 3rd argument of preg_split is the same as the one for explode, so you could write your code like this as well: $array = preg_split(‘#\s+#’, $string, 2); References: PHP: explode PHP: preg_split

Laravel belongsToMany exclude pivot table

Add pivot to your $hidden property’s array in your model(s). class Badge extends Eloquent { protected $hidden = [‘pivot’]; public function users() { return $this->belongsToMany(‘User’, ‘users_badges’); } } And same with your User model class User extends Eloquent { protected $hidden = [‘pivot’]; public function badges() { return $this->belongsToMany(‘Badge’, ‘users_badges’); } }

Whats the easiest way to determine if a user is online? (PHP/MYSQL)

Don’t bother with figuring out the differences between time zones—that’s not necessary. Whenever the user accesses a page, set/update a lastActiveTime field in their record of the Users table. Then, do a COUNT for all users having a lastActiveTime within the last 5 minutes. Anything more than this, and they can be considered “offline.” If … Read more

PHP – extended __construct

The parent __construct() method defined in class b will run automatically if you instantiate child class a, unless there is a __construct() method defined in class a. class a extends b { } class b { public function __construct() { echo ‘In B Constructor’; } } $x = new a(); If a __construct() method is … Read more

str_replace() for multiple value replacement

str_replace() accepts arrays as arguments. For example: $subject=”milk is white and contains sugar”; str_replace(array(‘sugar’, ‘milk’), array(‘sweet’, ‘white’), $subject); In fact, the third argument can also be an array, so you can make multiple replacements in multiple values with a single str_replace() call. For example: $subject = array(‘milk contains sugar’, ‘sugar is white’, ‘sweet as sugar’); … Read more