Skip to content

Instantly share code, notes, and snippets.

@ellm
Last active November 29, 2018 14:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ellm/9152981fc1c9d5342f1287e5a115546b to your computer and use it in GitHub Desktop.
Save ellm/9152981fc1c9d5342f1287e5a115546b to your computer and use it in GitHub Desktop.
Php Snippets and Notes

Php Snippets and Notes

printf

I need to escape/translate text that also requires HTML within in the copy. Use printf()

    <h3><?php printf( esc_html__( 'Get our exclusive newsletter—the best of %1$s The Dude %2$s every day!', 'localdude' ), '<span>', '</span>' ); ?></h3>

str_replace

I need to replace different parts of text in a string with new text. Example below replaces text in a embedded YouTube video src. Also uses explode and array_keys

<?php
    $return = '<iframe src="#"></iframe>>'; // example iframe code
    $id = explode( 'watch?v=', $url );
    $replace_keys = array(
        'allowfullscreen>' => 'allowfullscreen id="yt-' . $id[1] . '">',
        '?feature=oembed' => '?enablejsapi=1',
    );
    $html = str_replace( array_keys( $replace_keys ), $replace_keys, $return );
    return $html;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment