thebadbad
Members-
Posts
1,613 -
Joined
-
Last visited
Everything posted by thebadbad
-
[SOLVED] Remove only line breaks between [code] [/code]
thebadbad replied to Garrett's topic in Regex Help
Yeah, should've thought of that I know this, but I tend to always escape literal square brackets, to make it less confusing for less experienced regex'ers. -
FYI, preg_match_all() returns the number of full pattern matches.
-
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.'; } ?>
-
Something like <?php preg_match_all('~\bsrc\s?=\s?([\'"])(http://.+?)\1~is', $value, $matches); foreach ($matches[2] as $url) { if (strpos($url, 'pass') !== false) { echo "Match found: $url"; break; } } ?> You can use stripos() if the search should be case insensitive.
-
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'
-
[SOLVED] Remove only line breaks between [code] [/code]
thebadbad replied to Garrett's topic in Regex Help
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] -
[SOLVED] remove [quote]and everything in it [/quote]
thebadbad replied to golfromeo's topic in Regex Help
Add is after the last ~ to make the search case insensitive and to make the dot match line breaks. If it also should remove empty quote tags like , use an asterisk instead of the plus. ~\[quote\].*?\[/quote\]~is -
[SOLVED] Replace text within tags with formatted version
thebadbad replied to Garrett's topic in Regex Help
I would do something like this: <?php $str = '<a href="test.php">test</a> <br /> [nohtml] <a href="test2.php">test2</a> [/nohtml]'; $str = preg_replace_callback( '~\[nohtml\](.*?)\[nohtml\]~is', create_function( '$matches', 'return htmlentities($matches[1], ENT_QUOTES);' ), $str ); echo $str; ?> preg_replace_callback() @ the manual. -
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)
-
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/'); ?>
-
Are you using session_start()?
-
[SOLVED] Need help on a specific match, content of a "<pre>" tag ...
thebadbad replied to adrianTNT's topic in Regex Help
No it doesn't. But for some reason it escapes the quotes. When I use this instead, it doesn't: preg_replace('~(<pre\b[^>]*>)(.*?)(</pre>)~ise', '"$1" . htmlentities("$2") . "$3"', $my_text); -
[SOLVED] Need help on a specific match, content of a "<pre>" tag ...
thebadbad replied to adrianTNT's topic in Regex Help
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. -
Absolutely right. But why ask when you can try it out in like 1 sec?
-
In your sample code you're using $path before you've set it?
-
Could also have something to do with the character encoding. If you post a file including a sample string with the odd characters, we can test your code and see our results.
-
How can i save the newly created div with texts?
thebadbad replied to colap's topic in PHP Coding Help
Could be done with a database and AJAX. Or without AJAX, if you can live with it reloading the page on submit. -
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(''', "'", $str); or by using html_entity_decode(), but that will convert all HTML entities to the characters they represent.
-
Lol, I was about to post this, but then you beat me to it, haha:
-
So have you considered using sleep() within your loop?
-
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.
-
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.
-
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.