In trying to figure out how to do it with preg_match, but I'm left with no clue.
So what I'm trying is
$string = 'abc.hello.world.123.hola';
if ($string have 3 or more ".") {
echo 'true';
}
I know you said preg_match, but there is a specific PHP function for this. Use substr_count() :)
$string = 'abc.hello.world.123.hola';
$dots = substr_count($string, '.');
echo $dots; // output: 4
if ($dots >= 3) {
// do something
}
Related
Say I have string such as below:
"b<a=2<sup>2</sup>"
Actually its a formula. I need to display this formula on webpage but after b string is hiding because its considered as broken anchor tag. I tried with htmlspecialchars method but it returns complete string as plain text. I am trying with some regex but I can get only text between some tags.
UPDATE:
This seems to work with this formula:
"(c<a) = (b<a) = 2<sup>2</sup>"
And even with this formula:
"b<a=2<sup>2</sup>"
HERE'S THE MAGIC:
<?php
$_string = "b<a=2<sup>2</sup>";
$string = "(c<a) = (b<a) = 2<sup>2</sup>";
$open_sup = strpos($string,"<sup>");
$close_sup = strpos($string,"</sup>");
$chars_array = str_split($string);
foreach($chars_array as $index => $char)
{
if($index != $open_sup && $index != $close_sup)
{
if($char == "<")
{
echo "<";
}
else{
echo $char;
}
}
else{
echo $char;
}
}
OLD SOLUTION (DOESN'T WORK)
Maybe this can help:
I've tried to backslash chars, but it doesn't work as expected.
Then i've tried this one:
<?php
$string = "b<a=2<sup>2</sup>";
echo $string;
?>
Using < html entity it seems to work if i understood your problem...
Let me know
Probably you can give spaces such as :
b < a = 2<sup>2</sup>
It does not disappear the tag and looks much more understanding....
You could try this regex approach, which should skip elements.
$regex = '/<(.*?)\h*.*>.+<\/\1>(*SKIP)(*FAIL)|(<|>)/';
$string = 'b<a=2<sup>2</sup>';
$string = preg_replace_callback($regex, function($match) {
return htmlentities($match[2]);
}, $string);
echo $string;
Output:
b<a=2<sup>2</sup>
PHP Demo: https://eval.in/507605
Regex101: https://regex101.com/r/kD0iM0/1
I'm sorry, I just found a new problem due to my question about:
Get The Number of Sepecific String Inside String.
I have been trying hard, how to find the number of specific character inside a string? The case something like this.
function get_num_chars($char) {
$string = '120201M, 121212M-1, 21121212M, 232323M-2, 32323K, 323232K-1'
}
If I pass get_num_chars(M) would return 2
If I pass get_num_chars(M-1) would return 1
I tried count_chars(), substr_count() but it doesn't work.
It is possible with substr_count().
I think you are not passing a value to it properly. Try it like this:
$string = '120201M, 121212M-1, 21121212M, 232323M-2, 32323K, 323232K-1';
echo substr_count($string, 'K-1'); //echo's 1
echo substr_count($string, 'K'); // echo's 2
A possible solution using regular expression:
function get_num_chars($char) {
$string = '120201M, 121212M-1, 21121212M, 232323M-2, 32323K, 323232K-1';
return preg_match_all('/'.$char.'(?=,|$)/', $string, $m);
}
echo get_num_chars('M'); // 2
echo get_num_chars('M-1'); // 1
echo get_num_chars('M-2'); // 1
echo get_num_chars('K'); // 1
echo get_num_chars('K-1'); // 1
$k_count = substr_count($string, 'K') - substr_count($string, 'K-');
$k1_count = substr_count($string, 'K-1');
or
Counting K not followed by dash as follows:
$k_count = preg_match_all('/*K(?!-)/*', $string, $out);
Try this. It might not be feasible, but it addresses your problem.
function get_num_chars($char) {
$string = '120201M, 121212M-1, 21121212M, 232323M-2, 32323K, 323232K-1';
echo substr_count($string, $char);
}
get_num_chars('M,');
get_num_chars('M-1');
get_num_chars('k,');
Try this:
$string = '120201M, 121212M-1, 21121212M, 232323M-2, 32323K, 323232K-1';
$wordCounts = array_count_values(str_word_count($string,1));
echo $mCount = (isset($wordCounts['M-'])) ? $wordCounts['M'] : 0;
But here is one thing. You can just pass the 'M-' or 'M', not 'M-1'. It is some workaround for what you want. Because str_word_count matches the exact word count being used.
I need to convert lib_someString to someString inside a block of text using str_replace [not regex].
Here's an example to give an exact sense what I mean: lib_12345 => 12345. I need to do this for a bunch of instances in a block of text.
Below is my attempt. Problem I'm getting is that my function is not doing anything (I just get lib_id returned).
function extractLibId($val){ // function to get the "12345" in the above example
$lclRetVal = substr($val, 5, strlen($val));
return $lclRetVal;
}
function Lib($text){ // does the replace for all lib_ instances in the text
$lclVar = "lib_";
$text = str_replace($lclVar, "<a href='".extractLibId($lclVar)."'>".extractLibId($lclVar)."</a>", $text);
return $text;
}
Regexp gonna be faster and more clear, you will have no need to call your function for every possible 'lib_' string:
function Lib($text) {
$count = null;
return preg_replace('/lib_([0-9]+)/', '$1', $text, -1, $count);
}
$text = 'some text lib_123123 goes here lib_111';
$text = Lib($text);
Without regexp, but every time Lib2 will be called somewhere will die cute kitten:
function extractLibId($val) {
$lclRetVal = substr($val, 4);
return $lclRetVal;
}
function Lib2($text) {
$count = null;
while (($pos = strpos($text, 'lib_')) !== false) {
$end = $pos;
while (!in_array($text[$end], array(' ', ',', '.')) && $end < strlen($text))
$end++;
$sub = substr($text, $pos, $end - $pos);
$text = str_replace($sub, ''.extractLibId($sub).'', $text);
}
return $text;
}
$text = 'some text lib_123123 goes here lib_111';
$text = Lib2($text);
Use preg_replace.
Although it is possible to do what you need without regular expressions, you say you don't want to use them because of performance reasons. I doubt the other solution will be faster, so here is a simple regex to benchmark against:
echo preg_replace("/lib_(\w+)/", '$1', $str);
As shown here: http://codepad.org/xGj78r9r
Ignoring how ridiculous area of optimizing this is, even the simplest implementation with minimal validation already takes only 33% less time than a regex
<?php
function uselessFunction( $val ) {
if( strpos( $val, "lib_" ) !== 0 ) {
return $val;
}
$str = substr( $val, 4 );
return "{$str}";
}
$l = 100000;
$now = microtime(TRUE);
while( $l-- ) {
preg_replace( '/^lib_(.*)$/', "$1", 'lib_someString' );
}
echo (microtime(TRUE)-$now)."\n";
//0.191093
$l = 100000;
$now = microtime(TRUE);
while( $l-- ) {
uselessFunction( "lib_someString" );
}
echo (microtime(TRUE)-$now);
//0.127598
?>
If you're restricted from using a regex, you're going to have difficult time searching for a string you describe as "someString", i.e. not precisely known in advance. If you know the string is exactly lib_12345, for example, then set $lclVar to that string. On the other hand, if you don't know the exact string in advance, you'll have to use a regex via preg_replace() or a similar function.
If this is the input string:
$input = 'In biology (botany), a "fruit" is a part of a flowering
plant that derives from specific tissues of the flower, mainly one or
more ovaries. Taken strictly, this definition excludes many structures
that are "fruits" in the common sense of the term, such as those
produced by non-flowering plants';
And now I want to perform a search on the word tissues and consequently return only a part of the string, defined by where the result is, like this:
$output = '... of a flowering plant that derives from specific tissues of the flower, mainly one or more ovaries ...';
The search term may be in the middle.
How do I perform the aforementioned?
An alternative to my other answer using preg_match:
$word = 'tissues'
$matches = array();
$found = preg_match("/\b(.{0,30}$word.{0,30})\b/i", $string, $matches);
if ($found == 0) {
// string not found
} else {
$output = $matches[1];
}
This may be better as it uses word boundaries.
EDIT: To surround the search term with a tag, you'll need to slightly alter the regex. This should do it:
$word = 'tissues'
$matches = array();
$found = preg_match("/\b(.{0,30})$word(.{0,30})\b/i", $string, $matches);
if ($found == 0) {
// string not found
} else {
$output = $matches[1] . "<strong>$word</strong>" . $matches[2];
}
User strpos to find the location of the word and substr to extract the quote. For example:
$word = 'tissues'
$pos = strpos($string, $word);
if ($pos === FALSE) {
// string not found
} else {
$start = $pos - 30;
if ($start < 0)
$start = 0;
$output = substr($string, $start, 70);
}
Use stripos for case insensitive search.
I've seen so many misuses of RegExp, I don't really like it :)
I have string (as a result of two str_replaces) that might look something like this:
.?This iš my ".stRiNg."!
|
V
--this-is-my---string---
Is there any way better than
$string = trim(preg_replace('/[-]+/u','-', $string),'-');
to get:
this-is-my-string
?
preg_replace() wins
<?php
function benchmark($callback){
echo sprintf('%-30s: ', $callback);
$t = microtime(true);
foreach(range(1, 10000) as $n){
call_user_func($callback);
}
echo (microtime(true)-$t)."\n";
}
function implode_explode_filter(){
implode('-', array_filter(explode('-', '--this-is-my---string---')));
}
function preg_replace_trim(){
preg_replace('/-+/', '-', trim('--this-is-my---string---', '-'));
}
function brant(){
$parts = explode("-",'--this-is-my---string---');
for($i=0;$i<count($parts);$i++) {
if (strlen($parts[$i]) < 1) {
unset($parts[$i]);
}
}
reset($parts);
$string = implode("-",$parts);
}
function murze_bisko(){
$string = "--this-is-my---string---";
while (strpos($string, "--") !== false) {
$string = str_replace("--", "-", $string);
}
$string = trim($string, '-'); # both of their answers were broken until I added this line
}
benchmark('implode_explode_filter');
benchmark('preg_replace_trim');
benchmark('brant');
benchmark('murze_bisko');
# Output
# implode_explode_filter : 0.062376976013184
# preg_replace_trim : 0.038193941116333
# brant : 0.11686086654663
# murze_bisko : 0.058025121688843
?>
I don't understand why you are looking for a 'better' way. Your way uses a simple regex in a place where it's perfectly appropriate to do so. What could be better than that?
print implode('-', array_filter( explode( '-', $string) ) );
Easy. :)
A better way? Probably not. Another way? Yes (essentially one-line version of Brant's answer):
implode('-', array_filter(explode('-', $string), function($s) { return strlen($s); }));
Note that you can't just use the naked array_filter reliably, because !!"0" == false in PHP.
$parts = explode("-",'--this-is-my---string---');
for($i=0;$i<count($parts);$i++) {
if (strlen($parts[$i]) < 1) {
unset($parts[$i]);
}
}
reset($parts);
$string = implode("-",$parts);
So you want to go from
--this-is-my---string---
to
this-is-my-string?
The best way would be a reg exp, but if you want a simple solution in php you can use
$string = "--this-is-my---string---";
while (! strpos($string, "--") === false) {
$string = str_replace("--", "-", $string);
}
After the loop $string will contain the result.
Just for the sake of the competition:
$string = str_replace('--','-', str_replace('--','-', '--this-is-my---string---'));
This is the fastest way to do it. It first replaces double -- with single and then cleans the rest again, so only single - remain.