Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need to delete words bettwen slashes
I have this string:
This a test UP/PL/EX/TU 2013
this a test 2 MG/MF/RS/TB 2007
I need this output
This a test 2013
this a test 2 2007
The string is dinamically, always changes.
CAN BE DONE WHIT REGEX ?
I'm sure there's a better expression but given the strings in the question this might be good enough.
$string='This a test UP/PL/EX/TU 2013';
$output=preg_replace("/\s[\w\/]+\s/", " ", $string);
echo $output;
$s1 = 'This a test UP/PL/EX/TU 2013';
$s2 = ' this a test 2 MG/MF/RS/TB 2007';
$regex = '|\s*(?:[[:alnum:]]+/)+[[:alnum:]]+\s*|';
echo "$s1 => '", preg_replace($regex, ' ', $s1), "\n";
echo "$s2 => '", preg_replace($regex, ' ', $s2), "\n";
Output:
This a test UP/PL/EX/TU 2013 => 'This a test 2013
this a test 2 MG/MF/RS/TB 2007 => ' this a test 2 2007
HTH
You can use this:
$result = preg_replace('~\h*+\w*+\/(?>\w+\/?)++\h*+~', ' ', $string);
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I'm currently working on a search function for my website.
When the user is searching 'Test', and someone's name is 'besttest', for example, the 'test' in 'besttest' should be in another color. But only the 'test', not the whole word.
I've tried to figure it out myself, but I didn't get it working.
Hope you guys and girls can help me ^^
Use the preg_replace() function. The example below will highlight just the keywork wish is "Test" in this case not the whole word. Try this code, really it will help you.
$str = "besttest";
$keyword = "Test";
$str = preg_replace("/($keyword)/i",'<span class="yellow">$1</span>',$str);
print($str);
this will output something like this :
best<span class="yellow">test</span>
It makes only sense, if you use a markup language... but here's an approach:
$lookup = [
'test' => '<span class="red">%s</span>',
'color' => '<span class="green">%s</span>',
'[0-9]+' => '<b>%s</b>',
];
$string = 'This is a test for coloring testwise an uncolored test-value. Testing 950 349 2 numbers... ';
echo 'before: "'. $string.'"'.PHP_EOL;
foreach($lookup as $term => $format) {
$string = preg_replace_callback('/'.$term.'/', function($matches) use($format) {
return sprintf($format, $matches[0]);
}, $string);
}
echo 'after: "'. $string.'"'.PHP_EOL;
Output:
before: "This is a test for coloring testwise an uncolored test-value. Testing 950 349 2 numbers... "
after: "This is a <span class="red">test</span> for <span class="green">color</span>ing <span class="red">test</span>wise an un<span class="green">color</span>ed <span class="red">test</span>-value. Testing <b>950</b> <b>349</b> <b>2</b> numbers... "
With regular expressions, you can match pretty much anything possible and give it different styles...
hth
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Fetch Bill no from a string (output from tesseract OCR)
Tesseract OCR string is as follows
1;FTC013233
259139 Bill Date 23/06/2015
Mrs. DR.Greesshma-‘H Age/sex 23;y 22;D 1 Fema|e
Bill No 34939
Hospital ' Req No HG-4 1142645
3;HASH'KA'-A- D 9 %
Eergncy
VH)
a.. . !‘:‘u"‘_‘i"
Total Amount:
Paid Amount :
You can use preg_match along with Positive Lookbehind regex as
preg_match('/(?<=Bill\sNo\s)(\d+)\b/',$str,$res);
echo $res[0];//34939
You can use regex
/Bill\s+No\s+(\d+)/ig
Test here
PHP CODE :
<?php
$re = "/Bill\\s+No\\s+(\\d+)/i";
$str = "1;FTC013233\n\n259139 Bill Date 23/06/2015\nMrs. DR.Greesshma-‘H Age/sex 23;y 22;D 1 Fema|e\n\nBill No 34939\nHospital ' Req No HG-4 1142645\n\n3;HASH'KA'-A- D 9 %\n\n Eergncy\n\nVH)\n\na.. . !‘:‘u\"‘_‘i\"\n\nTotal Amount:\n\nPaid Amount :1;FTC013233\n\n259139 Bill Date 23/06/2015\nMrs. DR.Greesshma-‘H Age/sex 23;y 22;D 1 Fema|e\n\nBill No 34930999\nHospital ' Req No HG-4 1142645\n\n3;HASH'KA'-A- D 9 %\n\n Eergncy\n\nVH)\n\na.. . !‘:‘u\"‘_‘i\"\n\nTotal Amount:\n\nPaid Amount :";
preg_match_all($re, $str, $matches);
print_r($matches);
?>
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have a variable named $html:
$html = '<li class="col">[gallery_id=234]</li>';
I want to get gallery_id (in this case - 234) into another variable from $html.
Easy. Just use regex with preg_match:
$html = '<li class="col">[gallery_id=234]</li>';
preg_match("/\[gallery_id=([0-9].*)\]/is", $html, $matches);
echo '<pre>';
print_r($matches);
echo '</pre>';
I have that print_r in there for debugging/illustration purposes. It would return the following:
Array
(
[0] => [gallery_id=234]
[1] => 234
)
Then to access the result you want, just do this:
echo $matches[1];
The returned value will be:
234
$html = '<li class="col">[gallery_id=234]</li>';
preg_match('!\d+!', $html, $var);
echo $var[0]; //echoes 234
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I array text from custom field "black, grey, white" with this code:
<?php global $wp_query; $postid = $wp_query->post->ID; echo get_post_meta($postid, 'colors', true); ?>
I want show me like this:
black</br>
grey</br>
white
It's possible with PHP? Many thanks
Use str_replace():
echo str_replace(",", "<br />", get_post_meta($postid, 'colors', true));
If you want to add <br> after a "," you can do this:
$text = preg_replace("/,/", "<br>", get_post_meta($postid, 'colors', true));
str_replace(", " , ", <br />" , $string) ;
The format is ("from", "to" , "what")
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am designing a small application which shoots out status-like tweets from users at the five latest ones. To keep it a small application, I need for it to accept only so many characters on a line and the drop a line just below it. So for example:
Noah: The best thing about Stackoverflow
is that it is full of amazing programmers.
Something along the lines of something like that above. Can you help me with the code below :
echo "<div style='position:relative;top:-20px;padding-top:10px;padding-left:30px;padding-right:30px;'>";
echo "<p> $first_name: $body. </p>";
if (strlen($body <= 100)) {
echo "\n";
}
echo "</div>";
}
wordwrap will do that for you
string wordwrap ( string $str [, int $width = 75 [, string $break = "\n" [, bool $cut = false ]]] )
Example:
$text = "A very long woooooooooooord.";
$newtext = wordwrap($text, 8, "\n", true);
echo "$newtext\n";
A very
long
wooooooo
ooooord.