Jump to content

thebadbad

Members
  • Posts

    1,613
  • Joined

  • Last visited

Posts posted by thebadbad

  1. Except I think you can simplify your pattern by eliminating all captures (of course, this assumes that there is no newlines / carriage returns within the code tags themselves  :shy:)

     

    Yeah, should've thought of that :)

     

    A minor side note regarding escaped characters; some escape sequences can be avoided (of course, this is no big deal though)

     

    I know this, but I tend to always escape literal square brackets, to make it less confusing for less experienced regex'ers.

  2. If you're searching for a plain string, use strpos() or stripos() for case insensitivity:

     

    <?php
    if (strpos($var, 'srv_9 (Dead ???)') !== false) {
    echo '$var contains the searched string.';
    }
    ?>

  3. I'd go along with what cags has said. If you want to match using the preg_match_all I'd use '%<b(\b[^>]*)?>%i' as your regex

     

    Just a small note; you can remove the optional capture since the word boundary will match even if the following character class (quantized with the asterisk) matches zero times.

     

    '~<b\b[^>]*>~i'

  4. My approach:

     

    <?php
    $str = 'Text with a
    linebreak
    [code=test]test
    test
    test

    test2
    test2
    
    test2
    test2

    ';

    $str = preg_replace_callback(

    '~(\

    [code[^\]]*\])(.*?)(\[/code\])~is',
    create_function(
    '$matches',
    'return $matches[1] . str_replace(array("\r\n", "\r", "\n"), \'<br />\', $matches[2]) . $matches[3];'
    ),
    $str
    );
    //run your bbcode function here, including nl2br()
    echo $str;
    ?>[/code]

  5. Another thing to take into account is if you wish to handle nested quotes, which I'd imagine you quite likely do. Prepare for headaches :)

     

    You can quite easily support nested tags by dealing with the opening and closing tags separately. Relevant posts: http://www.phpfreaks.com/forums/index.php/topic,205904.msg934306.html#msg934306 and http://www.phpfreaks.com/forums/index.php/topic,266663.msg1259603.html#msg1259603 (seeing if the tags match up)

  6. Can't see why this shouldn't work:

     

    <?php
    if (isset($_GET['url'])) {
    $converter = array(
    	'blankspot' => 'blanks',
    	'soccer' => '906',
    	'blankspothere' => 'blank'
    );
    $url = $_GET['url'];
    
    if (isset($converter[$url])) {
    	$xml = @simplexml_load_file('/flv_player/data/playerConfigEmbed/' . $converter[$url] . '.xml');
    	if ($xml) {
    		echo (string) $xml->media->content->video['SD'];
    		exit;
    	}
    }
    }
    header('Location: http://www.sqweasel.com/');
    ?>

  7. Yep, just add the s modifier after the pattern (I also added a word boundary and made the search case insensitive):

     

    preg_replace('~<pre\b[^>]*>(.*?)</pre>~ise', 'htmlentities(\'$1\')', $my_text);

    If you want to retain the pre tags around the matched content, just grab the tags and use them in the replacement:

     

    preg_replace('~(<pre\b[^>]*>)(.*?)(</pre>)~ise', '\'$1\' . htmlentities(\'$2\') . \'$3\'', $my_text);

    I'm grabbing the last match to be sure the casing is right. Guess it's not necessary though.

  8. 1. Does slashes effect the function I've used above?

    2. Does it matter what I'm copying and pasting. ie if I were to copy and paste text which had an apostrophe written as &#39; would that effect how my function handles it?

     

    1. No.

    2. If your apostrophes are represented with HTML entities, that's where your problem lies. When you output your sample string, check the source code of it, and see if you find any HTML entities. If you do, you can simply replace them with something like

     

    $str = str_replace('&#39;', "'", $str);

     

    or by using html_entity_decode(), but that will convert all HTML entities to the characters they represent.

  9. Lol, I was about to post this, but then you beat me to it, haha:

     

    Then you don't need \K at all, but just

     

    $input = preg_replace('~<content[^>]*>~i', '', $input);

    And my advance apologies goes to nrg, who is probably in the process of writing an elaborate answer (no offence - you do a great job explaining things in detail, while my answers just often aren't that long). ;)

  10. Those are just the pattern delimiters of my preference. They can be any non-white space, non-alphanumeric ASCII character (except a backslash). And it's always smart to use delimiters that don't appear within the pattern, so you don't have to escape them, hence my use of the 'odd' tildes (I have yet to write a pattern matching a tilde). When you're using forward slashes as pattern delimiters, you would have to escape any forward slashes inside the pattern - something that occurs quite often.

  11. Edited your patterns a bit:

     

    $text = preg_replace('~\S+@\S+\.[a-z]+~i', '<a href="mailto:$0">$0</a>', $text);
    $text = preg_replace('~(^|\s)@([a-z0-9_]+)~i', '$1@<a href="http://twitter.com/$2" target="_blank">$2</a>', $text);

    The email one being this simple will also convert invalid emails, but it should at least work properly now.

  12. Try something like

     

    <?php
    if (preg_match('~MSIE ([^;]+);~', $_SERVER['HTTP_USER_AGENT'], $match)) {
    echo 'Internet Explorer version: ' . htmlentities($match[1]);
    } else {
    echo 'Browser isn\'t Internet Explorer';
    }
    ?>

     

    I'm using htmlentities() because the user agent string can be spoofed by the user, potentially containing malicious code to be executed.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.