Nicklas
Members-
Posts
111 -
Joined
-
Last visited
Never
Everything posted by Nicklas
-
Noobie Question: Splitting a string into an array
Nicklas replied to MortimerJazz's topic in PHP Coding Help
you dont need strlen, it´s enough with: [code=php:0]substr($postcode, 0, -3);[/code] -
The [b]e[b] modifier is missing in preg_replace() change to this: [code=php:0]echo preg_replace("/$searchterm/ise", 'highlight("\\0")', $result);[/code]
-
[quote]why you call the highlight function with highlight("\\0")?[/quote] \\0 is a backreference and holds the matched keyword(s) [quote]Because now the keywords in the result instead of highlight they get replaced with "highlight ("keywords")" string!!!![/quote] Show me your code.
-
the preg_replace call the function highlight() and takes the match, split it and then surround each word with a <span> tag. Then the string is put back together via the $tmp variable and return it to preg_replace().
-
do something like this if you want to highlight them individually: [CODE]<?php function highlight($str) { $tmp = ''; foreach(explode(' ', $str) as $word) $tmp .= "<span style=\"background-color:yellow\">$word</span> "; return trim($tmp); } $query = "test query"; $string = "hello there, this is a test query"; echo preg_replace("/$query/ise", 'highlight("\\0")', $string); ?>[/CODE]
-
http://www.phpfreaks.com/forums/index.php/topic,115226.msg469086.html#msg469086
-
better to use [ and ], { and } will be out of use in PHP6
-
[CODE]<?php $str = 'hello http://website1.com some word http://website2.com whatever else'; $spam = substr_count(strtolower($str), 'http://') - 1; echo $spam; // Result: 1 ?>[/CODE]
-
[code=php:0]echo preg_replace('/(""").*?\1/s', '<span style="color: blue">\\0</span>', $text);[/code]
-
correct me if i'm wrong, but if I remember correctly, the maximum allowed filesize is 2Mb. If your files go beond that, they wont be uploaded unless you edit your php.ini to allow bigger filesizes.
-
How do you store this list? database, textfile... ?
-
Here´s another way of displaying the first 10 words: [code=php:0]<?php $text = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."; list($words) = explode(' |', preg_replace('/(.*?\s+){10}/s', '\\0|', $text)); echo $words; ?>[/code]
-
I would use [url=http://www.php.net/preg_split]preg_split()[/url] to do the job. ex [code=php:0]list($artist, $title) = preg_split('/ - | \(/', $info);[/code] Then I just need to do something like this to loop thru my mp3 files and get the artist + title at the same time: [hr] [code=php:0]foreach(glob("some_dir/*.mp3") as $info) { list($artist, $title) = preg_split('/ - | \(/', $info); // Do what you want here... }[/code][hr]
-
Take a look at [url=http://www.php.net/strtotime]strtotime()[/url] ex [CODE]<?php $date = '2007-12-01'; echo date("m-d-Y", strtotime($date)); ?>[/CODE]
-
You could do something like this: [code=php:0]$str = 'bla bla bla...'; // select all <font> tags from the $str string preg_match_all('/(<font[^>]*>)/is', $str, $match); $match = $match[0]; // remove all tags with the size option foreach($match as $key => $tag) { if (preg_match('/size=("|\')?(\d+)?("|\')?/is', $tag)) unset($match[$key]); } // present all matched <font> tags print_r($match);[/code]
-
You could do something like this [code]<?php // a callback-function used by preg_replace() function strong($match, $word) { // Replace all <> tags and bbcode [] stuff with | (pipe) chars $tmp = preg_replace('/(<|\[).*?(>|])(.*?(<|\[)\s*\/.*?(>|]))?/s', '|', $match); // Surround the target word with <strong> tags $tmp = preg_replace('/\b' . $word . '\b/is', '<strong>\\0</strong>', $tmp); // Match all <> tags and bbcode [] stuff from the un-edited $match var preg_match_all('/(<|\[).*?(>|])(.*?(<|\[)\s*\/.*?(>|]))?/s', $match, $tag); // Put all the tags back into $tmp by replacing the | chars with the tags stored in the $tag array foreach($tag[0] as $html) { $tmp = preg_replace('/\|/', $html, $tmp, 1); } // ... and return the string return $tmp; } // the word you want to replace $word = "text"; // ...and the string $str = 'bla bla text <p>here is some text <a href="text.php">text link</a> and a bit <i>more text</i> here</p>. End of text.'; // Replace all $word's using a callback-function $str = preg_replace('/(?<=<p>).*?\b' . $word . '\b.*?(?=<\/p>)/ise', 'strong("\\0", $word)', $str); // and the result is... echo $str; ?>[/code]
-
There´s no need for regular expressions to do what you want, use explode() instead ex [code=php:0]$str = '15e-6812'; list( ,$str) = explode('-', $str);[/code] $str now holds the value 6812
-
take a look at [url=http://www.php.net/strtotime]strtotime()[/url] ex [CODE]<?php $time = "12/19/06 04:27 am"; echo strtotime($time); ?>[/CODE]
-
use [b]ffmpeg[/b] to convert between movie formats. If you dont know what it is, search the forum or just google it ;) simple syntax example [color=blue][b]ffmpeg -i original.avi newfile.3gp[/b][/color]
-
[SOLVED] Help Please , To Change , to . ( SOLVED) Thanks
Nicklas replied to just me and php's topic in PHP Coding Help
As [b]kenrbnsn[/b] suggest, use [i]str_replace()[/i] instead. There´s no need for regular expressions. -
scan a folder and get names of all folders within that folder?
Nicklas replied to ztealmax's topic in PHP Coding Help
you can use glob() with the [i]GLOB_ONLYDIR[/i] parameter ex [code]<?php $path = "themes"; // path to the dir you want to scan foreach(glob("$path/*", GLOB_ONLYDIR) as $dir) { echo basename($dir) . "<br />\n"; } ?>[/code] -
[SOLVED] Need some quick help extracting data from a string
Nicklas replied to moola's topic in Regex Help
Use explode() ex [hr] [code=php:0]$str = "Test Location: United States - South Carolina - Greenville - Bellsouth.net In"; list( , $state, $city) = explode(' - ', $str);[/code] [hr] Now the state is stored in [b]$state[/b], and the city in [b]$city[/b] ;) -
You can do it with regular expressions. ex [code=php:0]echo preg_replace('~(.*?<br />.*?)<br />~s', '\\1', $str);[/code]
-
ex [CODE]<?php $str = "audun is doing his homework;;http://www.audun.com/test.php;whenever he feels like it"; $array = preg_split('/;+/', $str); print_r($array); ?>[/CODE]
-
here´s another way [CODE]<?php $reply = "Yes I am able to go to the pub tommorrow"; echo strlen(preg_replace('/[^a]/is', '', $reply)); ?>[/CODE]