将 preg_replace 更改为 preg_replace_callback
Changing preg_replace to preg_replace_callback
我已经为这个案例寻找了其他结果,但我自己无法修复它。如果有人可以帮助我将 preg_replace 替换为 preg_replace_callback
,我将不胜感激
preg_replace("/[-_]([a-z])/e", "ucfirst('\1')", ucwords($verb));
<?php
/** @see Zang_Exception **/
require_once 'Exception.php';
/** @see Zang_Schemas **/
require_once 'Schemas.php';
/**
*
* A Zang InboundXML wrapper.
*
* Please consult the online documentation for more details.
* Online documentation can be found at: http://www.zang.io/docs/api/inboundxml/
*
* --------------------------------------------------------------------------------
*
* @category Zang Wrapper
* @package Zang
* @author Nevio Vesic <nevio@zang.io>
* @license http://creativecommons.org/licenses/MIT/ MIT
* @copyright (2012) Zang, Inc. <support@zang.io>
*/
class Zang_InboundXML
{
/**
* InboundXML simple xml element container
*
* @var null|SimpleXmlElement
*/
protected $element;
/**
* Current child pointer. Used for nesting validations
*
* @var string|null
*/
protected $_currentChild = null;
/**
* Constructs a InboundXML response.
*
* @param SimpleXmlElement|array $arg:
* - the element to wrap
* - attributes to add to the element
* - if null, initialize an empty element named 'Response'
*/
public function __construct($arg = null) {
switch (true) {
case $arg instanceof SimpleXmlElement:
$this->element = $arg;
$this->_currentChild = strtolower($arg->getName());
break;
case $arg === null:
$this->element = new SimpleXmlElement('<Response/>');
$this->_currentChild = 'response';
break;
case is_array($arg):
$this->element = new SimpleXmlElement('<Response/>');
$this->_currentChild = 'response';
foreach ($arg as $name => $value) {
$this->_validateAttribute($name, 'response');
$this->element->addAttribute($name, $value);
}
break;
default: throw new Zang_Exception('InboundXML Invalid construction argument');
}
}
/**
* Converts method calls into InboundXML verbs.
*
* @return SimpleXmlElement A SimpleXmlElement
*/
public function __call($verb, array $args) {
/** convert verbs input like-this-one to LikeThisOne **/
$verb = preg_replace("/[-_]([a-z])/e", "ucfirst('\1')", ucwords($verb));
/** Let's first go check if the verb exists **/
$this->_validateVerb(ucfirst($verb));
/** Let's go validate nesting **/
$this->_validateNesting(ucfirst($verb));
list($noun, $attrs) = $args + array('', array());
if (is_array($noun)) list($attrs, $noun) = array($noun, '');
$child = empty($noun)
? $this->element->addChild(ucfirst($verb))
: $this->element->addChild(ucfirst($verb), $noun);
foreach ($attrs as $name => $value) {
/** Validation of verb attributes **/
$this->_validateAttribute($name, $verb);
$child->addAttribute($name, $value);
}
return new self($child);
}
/**
* Returns the object as XML.
*
* @return string The response as an XML string
*/
public function __toString() {
$xml = $this->element->asXml();
return str_replace(
'<?xml version="1.0" ?>',
'<?xml version="1.0" encoding="UTF-8" ?>',
$xml
);
}
/**
* Validate existance of the verb. Return true if exists, throw exception
* if fails.
*
* @param string $verb
* @throws Zang_Exception
* @return bool
*/
private function _validateVerb($verb) {
$schemas = Zang_Schemas::getInstance();
if(!$schemas->isVerb(ucfirst($verb))) {
$available_verbs = implode(', ', $schemas->getAvailableVerbs());
throw new Zang_Exception(
"Verb '{$verb}' is not a valid InboundXML verb. Available verbs are: '{$available_verbs}'"
);
}
return true;
}
/**
* Validate if previous child allows this verb to be its child.
*
* @param string $verb
* @return boolean
* @throws Zang_Exception
*/
private function _validateNesting($verb) {
$schemas = Zang_Schemas::getInstance();
if(!$schemas->isNestingAllowed(ucfirst($this->_currentChild), ucfirst($verb))) {
$nestable_verbs = implode(', ', $schemas->getNestableByVerbs(ucfirst($this->_currentChild)));
$current_verb = ucfirst($this->_currentChild);
$next_verb = ucfirst($verb);
throw new Zang_Exception(
"InboundXML element '{$current_verb}' does not support '{$next_verb}' element. The following elements are supported: '{$nestable_verbs}'."
);
}
return true;
}
/**
* Validate if attribute of verb exists. If not, throw exception, otherwise, return true.
*
* @param string $attr
* @param string $verb
* @return boolean
* @throws Zang_Exception
*/
private function _validateAttribute($attr, $verb) {
$schemas = Zang_Schemas::getInstance();
if(!$schemas->isValidAttribute($attr, ucfirst($verb))) {
$verb_attribuges = implode(', ', $schemas->getAvailableAttributes(ucfirst($verb)));
throw new Zang_Exception(
"Attribute '{$attr}' does not exist for verb '{$verb}'. Available attributes are: '{$verb_attribuges}'"
);
}
return true;
}
}
有点不清楚为什么您想使用另一个功能。 可能是因为您正在升级 php 并且不再支持 e
修饰符。您发布的示例代码 建议 您正在尝试将连字符和下划线连接的表达式转换为驼峰式大小写...
看看这个简单的演示:
<?php
$verb = 'this is a simple-test_sentence to convert.';
echo preg_replace_callback(
"/[-_]([a-z]+)/",
function($string) { return ucfirst($string[1]); },
ucwords($verb)
);
输出显然是:
This Is A SimpleTestSentence To Convert.
我已经为这个案例寻找了其他结果,但我自己无法修复它。如果有人可以帮助我将 preg_replace 替换为 preg_replace_callback
,我将不胜感激preg_replace("/[-_]([a-z])/e", "ucfirst('\1')", ucwords($verb));
<?php
/** @see Zang_Exception **/
require_once 'Exception.php';
/** @see Zang_Schemas **/
require_once 'Schemas.php';
/**
*
* A Zang InboundXML wrapper.
*
* Please consult the online documentation for more details.
* Online documentation can be found at: http://www.zang.io/docs/api/inboundxml/
*
* --------------------------------------------------------------------------------
*
* @category Zang Wrapper
* @package Zang
* @author Nevio Vesic <nevio@zang.io>
* @license http://creativecommons.org/licenses/MIT/ MIT
* @copyright (2012) Zang, Inc. <support@zang.io>
*/
class Zang_InboundXML
{
/**
* InboundXML simple xml element container
*
* @var null|SimpleXmlElement
*/
protected $element;
/**
* Current child pointer. Used for nesting validations
*
* @var string|null
*/
protected $_currentChild = null;
/**
* Constructs a InboundXML response.
*
* @param SimpleXmlElement|array $arg:
* - the element to wrap
* - attributes to add to the element
* - if null, initialize an empty element named 'Response'
*/
public function __construct($arg = null) {
switch (true) {
case $arg instanceof SimpleXmlElement:
$this->element = $arg;
$this->_currentChild = strtolower($arg->getName());
break;
case $arg === null:
$this->element = new SimpleXmlElement('<Response/>');
$this->_currentChild = 'response';
break;
case is_array($arg):
$this->element = new SimpleXmlElement('<Response/>');
$this->_currentChild = 'response';
foreach ($arg as $name => $value) {
$this->_validateAttribute($name, 'response');
$this->element->addAttribute($name, $value);
}
break;
default: throw new Zang_Exception('InboundXML Invalid construction argument');
}
}
/**
* Converts method calls into InboundXML verbs.
*
* @return SimpleXmlElement A SimpleXmlElement
*/
public function __call($verb, array $args) {
/** convert verbs input like-this-one to LikeThisOne **/
$verb = preg_replace("/[-_]([a-z])/e", "ucfirst('\1')", ucwords($verb));
/** Let's first go check if the verb exists **/
$this->_validateVerb(ucfirst($verb));
/** Let's go validate nesting **/
$this->_validateNesting(ucfirst($verb));
list($noun, $attrs) = $args + array('', array());
if (is_array($noun)) list($attrs, $noun) = array($noun, '');
$child = empty($noun)
? $this->element->addChild(ucfirst($verb))
: $this->element->addChild(ucfirst($verb), $noun);
foreach ($attrs as $name => $value) {
/** Validation of verb attributes **/
$this->_validateAttribute($name, $verb);
$child->addAttribute($name, $value);
}
return new self($child);
}
/**
* Returns the object as XML.
*
* @return string The response as an XML string
*/
public function __toString() {
$xml = $this->element->asXml();
return str_replace(
'<?xml version="1.0" ?>',
'<?xml version="1.0" encoding="UTF-8" ?>',
$xml
);
}
/**
* Validate existance of the verb. Return true if exists, throw exception
* if fails.
*
* @param string $verb
* @throws Zang_Exception
* @return bool
*/
private function _validateVerb($verb) {
$schemas = Zang_Schemas::getInstance();
if(!$schemas->isVerb(ucfirst($verb))) {
$available_verbs = implode(', ', $schemas->getAvailableVerbs());
throw new Zang_Exception(
"Verb '{$verb}' is not a valid InboundXML verb. Available verbs are: '{$available_verbs}'"
);
}
return true;
}
/**
* Validate if previous child allows this verb to be its child.
*
* @param string $verb
* @return boolean
* @throws Zang_Exception
*/
private function _validateNesting($verb) {
$schemas = Zang_Schemas::getInstance();
if(!$schemas->isNestingAllowed(ucfirst($this->_currentChild), ucfirst($verb))) {
$nestable_verbs = implode(', ', $schemas->getNestableByVerbs(ucfirst($this->_currentChild)));
$current_verb = ucfirst($this->_currentChild);
$next_verb = ucfirst($verb);
throw new Zang_Exception(
"InboundXML element '{$current_verb}' does not support '{$next_verb}' element. The following elements are supported: '{$nestable_verbs}'."
);
}
return true;
}
/**
* Validate if attribute of verb exists. If not, throw exception, otherwise, return true.
*
* @param string $attr
* @param string $verb
* @return boolean
* @throws Zang_Exception
*/
private function _validateAttribute($attr, $verb) {
$schemas = Zang_Schemas::getInstance();
if(!$schemas->isValidAttribute($attr, ucfirst($verb))) {
$verb_attribuges = implode(', ', $schemas->getAvailableAttributes(ucfirst($verb)));
throw new Zang_Exception(
"Attribute '{$attr}' does not exist for verb '{$verb}'. Available attributes are: '{$verb_attribuges}'"
);
}
return true;
}
}
有点不清楚为什么您想使用另一个功能。 可能是因为您正在升级 php 并且不再支持 e
修饰符。您发布的示例代码 建议 您正在尝试将连字符和下划线连接的表达式转换为驼峰式大小写...
看看这个简单的演示:
<?php
$verb = 'this is a simple-test_sentence to convert.';
echo preg_replace_callback(
"/[-_]([a-z]+)/",
function($string) { return ucfirst($string[1]); },
ucwords($verb)
);
输出显然是:
This Is A SimpleTestSentence To Convert.