2012-12-28 28 views
0

我發現preg_matchpreg_match_all但這些將一次只能使用一個正則表達式。是否有任何函數比較一個字符串與兩個正則表達式在一次在php

function match(){ 
    $pattern = array(
     '/^\-?\+?[0-9e1-9]+$/', 
     '/^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9 ]{14}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11})$/' 
    ); 
    $string = '234'; 
    $pattern_mod = import(",",$pattern); 
    preg_match($pattern_mod ,$string); 

這就是我想要做的

+2

正則表達式模式是什麼樣的?也許你可以簡單地在一個:) – PeeHaa

+3

你能給我們一個你想要做什麼的具體例子嗎?編寫一個函數名稱,放入你想使用的兩個正則表達式,然後用單詞和代碼描述你想看到的結果。 PeeHaa幾乎肯定是正確的,因爲你正在做的事情可以使用一個正則表達式來完成,但是直到你向我們展示你實際正在嘗試完成的事情之前,我們無法確定。 – Charles

回答

1

如果我已經「解密」正確你的問題,我想你有只是使用and(如果有同時匹配),or(如果必須匹配至少一個)運算符與preg_matchpreg_match_all php函數。
這是編程的寶寶:)

像這樣

$string='myString'; 

if((preg_match(pattern,$string) and (preg_match(otherPattern,$string)) 
{ 
//do things 
[...] 
} 

or 

if((preg_match(pattern,$string) or (preg_match(otherPattern,$string)) 
{ 
//do things 
[...] 
} 

patternotherPattern是你的正則表達式模式

+2

請添加文檔和/或代碼示例以向我們展示您的意思。然後,我將upvote :) :) – Jelmer

+0

@Jelmer_更新 – DonCallisto

+0

,但不幸的是我得到模式作爲數組如果((preg_match($模式[],$字符串){/ /一些代碼}我不想用於循環 –

2

你可以使用正則表達式前瞻,其中主題將首先嚐試匹配foo和他們嘗試以匹配酒吧。 像這樣:

$regexPass='/^(?=((.*[A-Za-z].*[0-9].*)|(.*[0-9].*[A-Za-z].*)))(.{6,})$/'; 

上述正則表達式表示,它必須在第一表達式((.*[A-Za-z].*[0-9].*)|(.*[0-9].*[A-Za-z].*)))在這種情況下字母數字與至少一個數字和一個福斯產品匹配,並將它們匹配至少6位數字。

在一個更簡單的方法,你可以匹配foo和他們有一個上結束

$regexPass='/^(?=.\*foo.\*)(.\*n)$/'; 
0

我有兩個選項,你可以使用任何選擇一個適合你(我使用和條件只是一個例子) -

1)

$subject = "abcdef";<br /> 
$pattern = '/^def/'; <br /> 
$result = preg_match($pattern, substr($subject,3)); <br/> 
$result1 = preg_match("/php/i", "PHP is the web scripting language of choice."); <br /> 
echo ($result && $result1)?"true" :"false" 

2)

echo (preg_match($pattern, substr($subject,3)) && preg_match("/php/i", "PHP is the web scripting language of choice."))?"true" :"false"; 

儘管兩者幾乎相同,但代碼的方式不同,請選擇一種適合您的口味的代碼。

相關問題