preg_match如果第一个字符是斜杠

时间:2016-03-21 08:07:01

标签: php preg-match

我试着找出一个字符串的第一个字符是斜杠。

我有这个字符串$mystring = "/var/www/html"; $test = preg_match("/^/[.]/", $mystring); if ($test == 1) { echo "ret = 1"; } else { echo "ret = 0"; }

ret = 0

但我总是得到.lhs

5 个答案:

答案 0 :(得分:3)

试试这个:

# A sample Gemfile
source "https://rubygems.org"

gem "rails", '~> 4.2'
gem 'sass-rails'
gem 'coffee-rails'
gem 'less-rails'
gem 'uglifier', '>= 1.0.3'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
gem 'therubyracer', platforms: :ruby


gem 'jquery-rails'
gem 'select2-rails'
gem 'rails_tokeninput'
gem 'bootstrap-datepicker-rails'
gem 'date_time_attribute'
gem 'rails-assets-listjs', '0.2.0.beta.4' # remember to maintain list.*.js plugins and template engines on update
gem 'i18n-js', '~> 3.0.0.rc8'
gem 'rails-i18n'

gem 'mysql2'
gem 'prawn'
gem 'prawn-table'
gem 'haml-rails'
gem 'kaminari'
gem 'simple_form'
gem 'inherited_resources'
gem 'localize_input', git: "git://github.com/bennibu/localize_input.git"
gem 'daemons'
gem 'twitter-bootstrap-rails', '~> 2.2.8'
gem 'simple-navigation', '~> 3.14.0' # 3.x for simple_navigation_bootstrap
gem 'simple-navigation-bootstrap'
gem 'ransack'
gem 'acts_as_tree'
gem 'rails-settings-cached'
gem 'resque'
gem 'whenever', require: false # For defining cronjobs, see config/schedule.rb
gem 'protected_attributes'
gem 'ruby-units'
gem 'attribute_normalizer'
gem 'ice_cube', github: 'wvengen/ice_cube', branch: 'issues/50-from_ical-rebased' # fork until merged
gem 'recurring_select'
gem 'roo', '~> 1.13.2'
gem 'spreadsheet'

# we use the git version of acts_as_versioned, and need to include it in this Gemfile
gem 'acts_as_versioned', github: 'technoweenie/acts_as_versioned'
gem 'foodsoft_wiki', path: 'plugins/wiki'
gem 'foodsoft_messages', path: 'plugins/messages'

# plugins not enabled by default
#gem 'foodsoft_uservoice', path: 'plugins/uservoice'


group :production do
  gem 'exception_notification'
end

group :development do
  gem 'sqlite3'
  gem 'mailcatcher'
  gem 'web-console', '~> 2.0'

  # allow to use `debugger` https://github.com/conradirwin/pry-rescue
  gem 'pry-rescue'
  gem 'pry-stack_explorer'

  # Better error output
  gem 'better_errors'
  gem 'binding_of_caller'
  # gem "rails-i18n-debug"
  # chrome debugging extension https://github.com/dejan/rails_panel
  gem 'meta_request'

  # Get infos when not using proper eager loading
  gem 'bullet'

  # Hide assets requests in log
  gem 'quiet_assets'

  # Deploy with Capistrano
  gem 'capistrano', '~> 3.2.0', require: false
  gem 'capistrano-rvm', require: false
  gem 'capistrano-bundler', '>= 1.1.0', require: false
  gem 'capistrano-rails', require: false
  # Avoid having content-length warnings
  gem 'thin'
end

group :development, :test do
  gem 'ruby-prof', require: false
end

group :test do
  gem 'rspec-rails'
  gem 'factory_girl_rails'
  gem 'faker'
  gem 'capybara'
  # webkit and poltergeist don't seem to work yet
  gem 'selenium-webdriver'
  gem 'database_cleaner'
  gem 'connection_pool'
  # need to include rspec components before i18n-spec or rake fails in test environment
  gem 'rspec-core', '~> 3.2'
  gem 'rspec-rerun'
  gem 'rspec-legacy_formatters'
  gem 'i18n-spec'
  # code coverage
  gem 'simplecov', require: false
  gem 'coveralls', require: false
end

答案 1 :(得分:3)

您只需使用strpos()

<?php
    $mystring = "/var/www/html";
    if(strpos($mystring,"/") === 0){
        echo "ret = 1";
    }else{
        echo "ret = 0";
    }
?>    

答案 2 :(得分:3)

您必须使用preg_match的任何理由?

你不能使用substr?

if (substr($mystring, 0, 1) == "/") {
  echo "ret= 1";
}

答案 3 :(得分:2)

如果您只想做这件事,那么您也可以这样做:

echo $mystring[0] == "/" ? "ret 1" : "ret 0";

无需真正使用其他功能。

答案 4 :(得分:-3)

你必须逃避模式中的 / ,因为你已经将它用作你的正则表达式,所以试试这个:

$mystring = "/var/www/html";
$test = preg_match("/^\//", $mystring);

if ($test == 1)
{
    echo "ret = 1";
}
else 
{
    echo "ret = 0";
}