simple preg_match regex needed - preg-match

I am trying to preg match for tags in my strings that contain #[anyNumbers:anyNumbers:anyLetters]
i am hoping to remove the #[] leaving everything inbetween as a string that i can later filter.
What would be the easiest way to accomplish this?
$str = 'Have you heard of the #[159208207468539:274:One Day without Shoes] (ODWS) campaign? ODWS is an annual initiative by #[8416861761:274:TOMS] to bring awareness around the impact a pair of shoes can have on a child's life.';
function gettag($text){
//
//$regex = "\#[([a-z0-9-:]*)\]";
//$match = preg_match("/^$regex$/", $text);
//return $match;
return preg_replace('/#\[(\d+:\d+:[a-zA-Z]+)\]/', '${1}', $text);
}
gettag($str);
returns
Have you heard of the #[159208207468539:274:One Day without Shoes]
(ODWS) campaign? ODWS is an annual initiative by 8416861761:274:TOMS
to bring awareness around the impact a pair of shoes can have on a
child's life.

<?php
$string = "#[123:456:abcZ]";
preg_match('/^#\[(([0-9]+:){2}[a-zA-Z]+)\]$/', $string, $matches);
echo $matches[1];

Using slightly modified version of your pattern, you can do this:
function gettag($text) {
return preg_replace('/#\[([ a-z0-9-:]*)\]/i', '${1}', $text);
}
If you'd like it to be even more specific, we can modify the pattern to be stricter:
function gettag($text) {
return preg_replace('/#\[(\d+:\d+:[a-zA-Z ]+)\]/', '${1}', $text);
}
See it on codepad

Related

Get first letter of word in statement

I have a statement like "Animal Association" from the database. I want to get its short form. It means, only the first letter of each word like this "AA". In the blade file, I got the whole statement as follows,
<p>{{ $animal->user->club->name}}</p>
So, how can I get a short form of this name?
Thank You!
If you are using MySQL 8+, then a raw select with REGEXP_REPLACE should work here:
$users = DB::table('animals')
->select(DB::raw("SELECT REGEXP_REPLACE(name, '(\\w)\\w+\\s*', '$1')"))
->get();
This very common problem where we ran into, I can provide you a function that will solve your problem. I am sharing two solutions and you can use any of these solutions.
using function
You can use this function in your model and solve your problem.
public function getNameAbbreviate($string){
$abbreviation = "";
$string = ucwords($string);
$words = explode(" ", "$string");
foreach($words as $word){
$abbreviation .= $word[0];
}
return $abbreviation;
}
There is probably no one-line solution, the solution which I provided is readable and understandable.
using regex
This solution is easy to apply and in case you can't make the first method work then go with this.
<p>{{ preg_split("/\s+/", $animal->user->club->name) }}</p>
using regex we can get a direct solution but I personally don't like it or recommend it.

Format Output of Placeholder

I am creating a dynamic list of placeholders, some of the values held in these place holders are decimal numbers that are supposed to represent money.
What I'm wondering is if there is a way I can format them to display as such?
Something like [[+MoneyField:formatmoney]]
I see http://rtfm.modx.com/revolution/2.x/making-sites-with-modx/customizing-content/input-and-output-filters-(output-modifiers) but I do not see a way to do this here.
You most definitely can, under the header "Creating a Custom Output Modifier" on the link you posted it's described how you can place a snippet name as a output modifier. This snippet will recieve the [[+MoneyField]] value in a variable called $input.
So you'd have to create this custom snippet which could be as simple as
return '$'.number_format($input);
Another version of doing this is calling the snippet directly instead of as an output modifier like so:
[[your_custom_money_format_snippet ? input=`[[+MoneyField]]`]]
I'm not sure if theres any difference between the two in this case. Obviously you can pass any value into the number format snippet when calling it as a snippet instead of an output modifier. And i'm sure theres a microsecond of performance difference in the two but i'm afraid i don't know which one would win. ;)
Update:
Actually found the exact example you want to implement on this link;
http://rtfm.modx.com/revolution/2.x/making-sites-with-modx/customizing-content/input-and-output-filters-%28output-modifiers%29/custom-output-filter-examples
Snippet:
<?php
$number = floatval($input);
$optionsXpld = #explode('&', $options);
$optionsArray = array();
foreach ($optionsXpld as $xpld) {
$params = #explode('=', $xpld);
array_walk($params, create_function('&$v', '$v = trim($v);'));
if (isset($params[1])) {
$optionsArray[$params[0]] = $params[1];
} else {
$optionsArray[$params[0]] = '';
}
}
$decimals = isset($optionsArray['decimals']) ? $optionsArray['decimals'] : null;
$dec_point = isset($optionsArray['dec_point']) ? $optionsArray['dec_point'] : null;
$thousands_sep = isset($optionsArray['thousands_sep']) ? $optionsArray['thousands_sep'] : null;
$output = number_format($number, $decimals, $dec_point, $thousands_sep);
return $output;
Used as output modifier:
[[+price:numberformat=`&decimals=2&dec_point=,&thousands_sep=.`]]

Ruby Regular Expression: Setting $1 variable in a hash

Everything in this code works properly, except the contents of the $1 variable aren't being properly displayed. According to my tests, all the matching is being done properly, I am just having trouble figuring out how to actually output the contents of $1.
codeTags = {
/\[b\](.+?)\[\/b\]/m => "<strong>#{$1}</strong>",
/\[i\](.+?)\[\/i\]/m => "<em>#{$1}</em>"
}
regexp = Regexp.new(/(#{Regexp.union(codeTags.keys)})/)
message = (message).gsub(/#{regexp}/) do |match|
codeTags[codeTags.keys.select {|k| match =~ Regexp.new(k)}[0]]
end
return message.html_safe
Thank you!
As soon as you do this:
codeTags = {
/\[b\](.+?)\[\/b\]/m => "<strong>#{$1}</strong>",
/\[i\](.+?)\[\/i\]/m => "<em>#{$1}</em>"
}
The #{$1} bits in the values are interpolated using whatever happens to be in $1 at the time. The values will most likely be "<strong></strong>" and "<em></em>" and those aren't very useful.
And regexp is already a regular expression object so gsub(/#{regexp}/) should be just gsub(regexp). Similar things apply to the keys of codeTags, they're already regular expression objects so you don't need to Regexp.new(k).
I'd change the whole structure, you're overcomplicating things. Just something simple like this would be fine for only two replacements:
message = message.gsub(/\[b\](.*?)\[\/b\]/) { '<strong>' + $1 + '</strong>' }
message = message.gsub(/\[i\](.*?)\[\/i\]/) { '<em>' + $1 + '</em>' }
If you try to do it all at once you'll have problems with nesting in something like this:
message = 'Where [b]is[/b] pancakes [b]house [i]and[/i] more[/b] stuff?'
You'd end up having to use a recursive gsub and possibly some lambdas if you wanted to properly handle things like that with a single expression.
There are better things to spend your time on than trying to be clever on something like this.
Response to comments: If you have more bb-tags and some smilies to worry about and several messages per page then you should HTMLify each message when you create it. You could store only the HTML version or both HTML and BB-Code versions if you want the BB-Code stuff around for some reason. This way you'd only pay for the HTMLification once per message and producing your big lists would be nearly free.

php xpath query on and xpath result

Can I use an xpath query on a result already obtained using xpath?
In most hosting languages/environments (like XSLT, XQuery, DOM) you can. Don't know about PHP, but it would be strange if it doesn't allow this.
Of course, the result of the first query must be a node-set, in order for a future "/" operator to be possible/allowed/successful on it.
I have done it in PHP/SimpleXML. The thing that I didn't understand at first is that you're still dealing with the full SimpleXML object, so if you start with "/nodename", you're operating on root. If you start with "nodename" you are starting at the beginning of the result node. Here's my example:
$parsed=simplexml_load_string($XML);
$s = '/ItemSearchResponse/Items/Item';
$items = $parsed->xpath($s);
foreach($items as $item)
{
$s = 'ItemAttributes/Feature';
$features[]=$item->xpath($s);
$s = 'ASIN';
$asins[]=$item->xpath($s);
$s = 'ImageSets/ImageSet[#Category="primary"]';
$primary_img_set=$item->xpath($s);
$s = 'MediumImage/URL';
$medium_image_url[] = $primary_img_set[0]->xpath($s);
}
In PHP, for example, you can run a query with a context, i.e. a given node. So if you have got a DOMNodeList as a result of the first query you can do things like this:
$query1 = '//p';
$query2 = './a'; // do not forget the dot
$node = $xpath->query($query1)->item(0);
$result = $xpath->query($query2, $node);
Of course this is a silly example because it could have been done just in one shot with the correct XPath experssion but I believe it illustrates your question.

Is there a Joomla function to generate the 'alias' field?

I'm writing my own component for Joomla 1.5. I'm trying to figure out how to generate an "alias" (friendly URL slug) for the content I add. In other words, if the title is "The article title", Joomla would use the-article-title by default (you can edit it if you like).
Is there a built-in Joomla function that will do this for me?
Line 123 of libraries/joomla/database/table/content.php implements JFilterOutput::stringURLSafe(). Pass in the string you want to make "alias friendly" and it will return what you need.
If you are trying to generate an alias for your created component it is very simple. Suppose you have click on save or apply button in your created component or suppose you want to make alias through your tile, then use this function:
$ailias=JFilterOutput::stringURLSafe($_POST['title']);
Now you can insert it into database.
It's simple PHP.
Here is the function from Joomla 1.5 source:
Notice, I have commented the two lines out. You can call the function like
$new_alias = stringURLSafe($your_title);
function stringURLSafe($string)
{
//remove any '-' from the string they will be used as concatonater
$str = str_replace('-', ' ', $string);
$str = str_replace('_', ' ', $string);
//$lang =& JFactory::getLanguage();
//$str = $lang->transliterate($str);
// remove any duplicate whitespace, and ensure all characters are alphanumeric
$str = preg_replace(array('/\s+/','/[^A-Za-z0-9\-]/'), array('-',''), $str);
// lowercase and trim
$str = trim(strtolower($str));
return $str;
}

Resources