How to extract img src, title and alt from html using php

preg_match_all match the regexp in all the $html string and output everything as
an array in $result. “i” option is used to make it case insensitive

preg_match_all('/<img[^>]+>/i',$html, $result);

print_r($result);

Get the metadata

$img = array();
foreach( $result as $img_tag)
{
preg_match_all('/(alt|title|src)=("[^"]*")/i',$img_tag, $img[$img_tag]);
}

print_r($img);

Here you go.

PHP XML tag contains colon

With PHPs’ simple xml functions it is possible to read xml easily. However, that doesn’t work any longer when a tag contains a colon. That tag is simply ignored and can’t be accessed. Often in RSS feeds there is content:encoded.

There is a trick. Instead of using simplexml_load_file use

$feed = file_get_contents($url);
$feed = str_replace("<content:encoded>", "<contentEncoded>", $feed);
$feed = str_replace("</content:encoded>", "</contentEncoded>", $feed);
$xml = simplexml_load_string($feed);

have fun!

The only difference is the behavior of auto escaping in twig

Imagine you have a var variable containing: <div>I'm happy</div>.

On index.twig, {{ var }} will render <div>I'm happy</div>.

On index.html.twig, {{ var }} will render &lt;div&gt;I&#039;m happy&lt;div&gt;

On index.js.twig, {{ var }} will render \x3Cdiv\x3EI\x27m\x20happy\x3Cdiv\x3E

And so on.

Always use the right extension to avoid any XSS vulnerability, and
always use |raw wisely because it overlaps this extension’s implicit protection.

How to paste / insert in vim from clipboard

When ever I tried to insert text from the clipboard to vim it did either not work at all or the lines were inserted like a tree, each line more indented.

To fix that, you can do the following:
Before pasting into vim, enable paste mode by entering :set paste.
Press I to enter insert mode. The status bar should say — INSERT (paste) — now.
Press Shift + Insert (The auto indentation of vim should not happen.)
Press Esc to leave insert mode, and disable paste mode using :set nopaste again.
This works from windows terminal, putty, linux terminal and WSL.

PHP sort array by key

Sorting an array by Key.

<?php
/*
Array
(
    [0] => Array
        (
            [hashtag] => a7e87329b5eab8578f4f1098a152d6f4
            [title] => Flower
            [order] => 3
        )

    [1] => Array
        (
            [hashtag] => b24ce0cd392a5b0b8dedc66c25213594
            [title] => Free
            [order] => 2
        )

    [2] => Array
        (
            [hashtag] => e7d31fc0602fb2ede144d18cdffd816b
            [title] => Ready
            [order] => 1
        )
)
*/
function aasort (&$array, $key) {
	$sorter = array();
	$ret = array();
	reset($array);
	foreach ($array as $ii => $va) {
	    $sorter[$ii] = $va[$key];
	}
	asort($sorter);
	foreach ($sorter as $ii => $va) {
	    $ret[$ii] = $array[$ii];
	}
	$array = $ret;
}
aasort($your_array,"order");
?>

__toString() must not throw an exception

__toString() must not throw an exception.

 

If you an exception handler even that seems fail (in itself) with __toString() must not throw an exception. There is no nice safe way to handle it. The easiest solution is to catch that error as well. It might seems to be odd, but on the other hand the exception hanler shall not fail ;)


/**
* String representation of this object
* @return string
*/
public function __toString()
{
try {
return (string) $this->name;
} catch (Exception $exception) {
return '';
}
}

PHP detect IOS / iPhone / iPad / iPod

Very simple

<?php
function is_ios(){
    if (stripos($_SERVER['HTTP_USER_AGENT'],"iPhone")  !== false) {
        return true;
    } elseif (stripos($_SERVER['HTTP_USER_AGENT'],"iPad") !== false) {
        return true;
    } elseif (stripos($_SERVER['HTTP_USER_AGENT'],"iPod") !== false) {
        return true;
    }
    return false;
}
?>

Archive for category php

Archives by Month: