ted_chou12 Posted April 9, 2011 Share Posted April 9, 2011 Hi, this is the config file: server.document-root = "/usb/sda1/http" [color=teal]server.port = 3001[/color] mimetype.assign = ( ".html" => "text/html", ".txt" => "text/plain", ".jpg" => "image/jpeg", ".png" => "image/png" ) dir-listing.encoding = "utf-8" dir-listing.activate = "enable" server.modules = ( "mod_access", "mod_accesslog", "mod_fastcgi", "mod_rewrite", "mod_auth", "mod_alias", "mod_compress", ) index-file.names = ( "index.php", "index.html", "index.htm", "default.htm", "index.lighttpd.html" ) fastcgi.map-extensions = ( ".php3" => ".php", ".php4" => ".php" ) server.errorlog = "/var/lighttpd.log" static-file.exclude-extensions = ( ".php", ".pl", ".fcgi" ) fastcgi.server = ( ".php" => (( "bin-path" => "/usb/sda1/php5-cgi/usr/bin/php5-cgi", "socket" => "/usb/sda1/php5-cgi/php.socket" ))) I want to match it with this code <?php $file = file_get_contents("test.txt"); preg_match("/^server/\./port\s=\s/", "$file", $matches); echo "|$matches[0]|"; ?> But the returned string is empty? In fact, I am not sure why it only matches up to the dot inbetween the server and the port? Thanks, Ted Quote Link to comment https://forums.phpfreaks.com/topic/233184-matching-for-port-number-in-config-file/ Share on other sites More sharing options...
salathe Posted April 9, 2011 Share Posted April 9, 2011 Your regex /^server/\./port\s=\s/ is broken, please turn on displaying of errors (ini_set('display_errors', true)) and allow all errors to be reported (error_reporting(-1)) and you will see that PHP tells you that the regex is broken with the message Warning: preg_match() [function.preg-match]: Unknown modifier '\' in …. I have absolutely no idea why you have the forward slashes around the dot part of the regex, but they are not needed so remove them: /^server\.port\s=\s/ Secondly, you want to match the port number but nowhere in the regex is looking for any sort of number at all! Specify that a) you want to find a port number, and b) that you want to capture it separately for use later. /^server\.port\s=\s(\d+)/ Thirdly, you ask to match only at the start of the subject string (with ^) where instead you want to match at the start of a line. To allow the caret (^) to match the start of a line, you need to set the "multiline" pattern modifier (docs): /^server\.port\s=\s(\d+)/m With the amended regex, you will now be able to use $matches[1] to get the port number. Quote Link to comment https://forums.phpfreaks.com/topic/233184-matching-for-port-number-in-config-file/#findComment-1199190 Share on other sites More sharing options...
ted_chou12 Posted April 9, 2011 Author Share Posted April 9, 2011 Thank you! I see, I am still struggling with regex syntax :/. Thanks! Ted Quote Link to comment https://forums.phpfreaks.com/topic/233184-matching-for-port-number-in-config-file/#findComment-1199194 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.