i need to remove whitespaces from the beginning and end of loop phrase
All words come from an loop, and look like this: " Hello all people "
I'm using the code -
$appliedFilters = Mage::getSingleton('catalog/layer')->getState()->getFilters();
foreach ($appliedFilters as $item) {
if ($item->getFilter()->getRequestVar() == 'b_car' || 'c_model' || 'd_year') {
$n_str = string.replace("\"", "", $item->getLabel()));
echo $n_str;
}
}
This code returns "Helloallpeople"
But i need "Hello all people"
Please help!
UPDATED
var_dump($item->getLabel()); returns string(7) "Hello " string(8) "all " string(5) "People "
Try Regx like the following:
$returnValue = preg_replace("/>\s+(.*)\s+</", '>$1<', '<a> Hello all people <a/>');
I am keeping the old answer for reference.
But if you simply want to remove leading and trailing spaces; use trim()
Update:
If you want to trim each and every element of an array; you can map trim function to it.
Then you can also implode the array to a string.
<?php
$str = array(" Hello all ", " Hello all people ", " all people ", " Hello people ");
$n_str = array_map("trim",$str);
var_dump($n_str);
echo implode(" ",$n_str);
?>
Update 2:
Ok I got it; It's not an array. It's a loop.
Every time $item->getLabel() returns just a string. it's not an array.
Following should help you.
$appliedFilters = Mage::getSingleton('catalog/layer')->getState()->getFilters();
$result = "";
foreach ($appliedFilters as $item) {
if ($item->getFilter()->getRequestVar() == 'b_car' || 'c_model' || 'd_year') {
$result .= " ".trim($item->getLabel());
}
}
echo trim($result);
Try this:
$words = $item->getLabel(); //array(" Hello", "all ", "people ");
echo trim(preg_replace("/\s+/", " ", implode($words, " ")));
// Output: Hello all people
See demo
Related
I had an value in database like "demo text" . I want to display this content from the db in the view page as
Html code that i am using is like this <h2>Demo<span>Text</span></h2> , is there any solution for seperate each words and use one for h2 and other for span. I am using php codeigniter for the project , I don't know that whether the way i explained my problem is correct or not .
Yes you can so it with explode
if you have stored at least 2 words with space. try following
$demo ="demo text";
$arr = explode(" ",$demo);
$str = "<h2>".$arr[0]."<span>".$arr[1]."</span></h2>";
echo $str;
DEMO
EDIT
If you have more words and want to split first word only you can pass limit parameter in explode
$demo ="Pligrimage to Marian Shrines";
$arr = explode(" ",$demo,2);
$str = "<h2>".$arr[0]."<span>".$arr[1]."</span></h2>";
echo $str;
DEMO
I think you are looking for something like this!
$your_string = "Hello Houston! We have a problem!";
$my_array = explode(" ",trim($your_string));
$output = "<h2>";
foreach($my_array as $a_word){
if ($a_word === reset($my_array))
$output .= $a_word;
else
$output .= " <span>". $a_word . "</span>";
}
$output .= "</h2>";
print $output;
The code below produces an unexpected result:
$row = 'one //There are spaces here
two ';
$row = explode(PHP_EOL,$row);
$row = $row[0];
preg_match('/( +)$/',$row,$matches);
When using var_dump($matches);, the output is:
array(0) {
}
But, it should be something like this:
array(2) {
[0]=>string(8) " "
[1]=>string(8) " "
}
Replacing PHP_EOL with "\n" makes no difference.
Using preg_match('/(\s+)$/',$row,$matches); produces the expected result:
array(2) {
[0]=>string(8) " "
[1]=>string(8) " "
}
The expected also happens when using:
$row = 'one ';
preg_match('/( +)$/',$row,$matches);
But, obviously, these both have their own reasons to not be used.
My question is: Why does PHP not recognize the spaces as spaces but only as whitespace?
Example: http://sandbox.onlinephpfunctions.com/code/a6f3ea8b422671d86a37b765986395b1dd6f94e8
You need to use "\n\r" instead of PHP_EOL or space
$row = explode("\n\r",$row);
$row = $row[0];
preg_match('/( +)$/',$row,$matches);
Now as you see you can use space in pattern for regExp
Anybody knows how i can achieve this:
I want post message and colorize everything between " * " Tags.
Like this:
This [*]is[*] test [*]message[*] :)
To:
This [yellow]is[/yellow]> test [yellow]message[/yellow] :)
I wroted something like this to achieve my goal:
if(preg_match_all('/\*(.*?)\*/',$message,$match)) {
$beforemessage = explode("*", $message, 2);
$message = $beforemessage[0]. " <font color='yellow'>" .$match[0][0]. "</font>";
}
Howewer it returns only:
This [yellow]is[yellow]
Just use preg_replace():
$message = "This *is* test *message*";
echo preg_replace('/\*(.*?)\*/', '<font color="yellow">$1</font>', $message);
This <font color="yellow">is</font> test <font color="yellow">message</font>
preg_match_all returns an array of matches, but your code only ever replaces the FIRST match in that array. You'd have to loop over the array to handle the OTHER matches.
There are a few approaches when using regular expressions.
One is to do matching - keeping track of the position of the match and the length of the match. Then you can split up the original message into substrings and concatenate it all back together.
The other is to do search/replace using regular expressions.
Try this, or similar approach:
<?php
$text = "Hello hello *bold* foo foo *fat* foo boo *think* end.";
$tagOpen = false;
function replaceAsterisk($matches) {
global $tagOpen;
$repl = "";
if($tagOpen) {
$repl = "</b>";
} else {
$repl = "<b>";
}
$tagOpen = !$tagOpen;
return $repl;
}
$result = preg_replace_callback( "/[*]/", "replaceAsterisk", $text);
echo $result;
I have some code running which finds out hashtags in the string and turns them into links. I have done this using preg_match_all as shown below:
if(preg_match_all('/(#[A-z_]\w+)/', $postLong, $arrHashTags) > 0){
foreach ($arrHashTags[1] as $strHashTag) {
$long = str_replace($strHashTag, ''.$strHashTag.'', $postLong);
}
}
Also, for my search script, I need to bold the searched keywords in the result string. Something similar to the below code using preg_replace:
$string = "This is description for Search Demo";
$searchingFor = "/" . $searchQuery . "/i";
$replacePattern = "<b>$0<\/b>";
preg_replace($searchingFor, $replacePattern, $string);
The problem that I am having is that both have to work together and should be thrown as a combined result. One way I can think of is to run the resultant string from preg_match_all with the preg_replace code but what if the tags and the searched string are the same? The second block will bold my tag as well which is not desired.
update the code i'm running based on the answer given below but it still doesn't work
if(preg_match_all('/(#[A-z_]\w+)/', $postLong, $arrHashTags) > 0){
foreach ($arrHashTags[1] as $strHashTag) {
$postLong = str_replace($strHashTag, ''.$strHashTag.'', $postLong);
}
}
And immediately after this, i run this
$searchingFor = "/\b.?(?<!#)" . $keystring . "\b/i";
$replacePattern = "<b>$0<\/b>";
preg_replace($searchingFor, $replacePattern, $postLong);
Just so you know, this is all going inside a while loop, which is generating the list
You just need to modify you the search pattern to avoid ones that start with a '#'
$postLong = "This is description for Search Demo";
if(preg_match_all('/(#[A-z_]\w+)/', $postLong, $arrHashTags) > 0){
foreach ($arrHashTags[1] as $strHashTag) {
$postLong = str_replace($strHashTag, ''.$strHashTag.'', $postLong);
}
}
# This expression finds any text with 0 or 1 characters in front of it
# and then does a negative look-behind to make sure that the character isn't a #
searchingFor = "/\b.?(?<!#)" . $searchQuery . "\b/i";
$replacePattern = "<b>$0<\/b>";
preg_replace($searchingFor, $replacePattern, $postLong);
Or if you don't need an array of the available hashes for another reason, you could use preg_replace only.
$postLong = "This is description for #Search Demo";
$patterns = array('/(#[A-z_]\w+)/', "/\b.?(?<!#)" . $searchQuery . "\b/i");
$replacements = array(''.$0.'', ' "<b>$0<\/b>');
preg_replace($patterns, $replacements, $postLong);
Still having RegEx issues.. Need to match the following characters
a-zA-z9-0 , . ' " ( ) _ - : (SPACE)
Not all the values will have all these but could have them. I have everything withing but the parentheses, Single and Double Quoytes
/^[\w. ,\/:_-]+$/
UPDATE:
I got it working with this: "/^[\w. ,:()'\"-]+$/"
$val_1 = "Abh acb 123 . - _ 's ";
$val_2 = "Asc";
$val_3 = "234";
$val_4 = "nj%"; // Fail
$val_5 = "Help (me)";
$val_6 = "What's wrong?"; // Fail
$val_7 = "She's here";
$val_8 = "No: 123.00, 432.00";
$val_9 = 'Need to " Double" ';
$var_array = array($val_1, $val_2, $val_3, $val_4, $val_5, $val_6, $val_7, $val_8, $val_9);
foreach ($var_array as $k=>$d) {
if ((preg_match("/^[\w. ,:()'\"-]+$/", $d))) {
echo "Yeah it matches!!!<span style='color:green'>".$d."</span><br />";
} else {
echo "Try again, thie FAILED<span style='color:red'>".$d."</span><br />";
}
}
Thanks for all for helping out
$pat = "/^[\w. ,\\/:_()'\"-]/";
To match all those, you just need:
preg_match("/[a-zA-Z0-9,.'\"()_- :]/", $string);
/^[-a-zA-Z0-9,.'"()_: ]+$/
This should work. But if you put it into a string be sure to escape the needed quotes.
With the help of the other submitting I have found the solution that works:
"/^[\w. ,:()'\"-]+$/"
Thanks to all for the help