Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have this string.
130003 lebtro is cool 609664 trochanter2 460936 bibi95 410635 MrRadical38 206311 Labonte 577767 thebanzaikid 569248 marco 355894 Armored_J 630259 SNEG323 569014 llama 501235 plummer 305212 lonefish 294268 dragonbleed 399281 Stelomat 619909 McPred 22733 Storykeeper 634691 The Max 504980 MrFrench 248285 Darkshadow2013 647361 Cain
And I want to be able to parse so the numbers followed by the letters like this:
130003 lebtro is cool<br>
609664 trochanter2<br>
460936 bibi95<br>
410635 MrRadical38<br>
206311 Labonte<br>
...<br>
...<br>
...<br>
647361 Cain
Would I need to use a RegEx in PHP or is there any other way to do it?
Here's a regex solution:
\b(\d+)\s+(.+?(?=\s+\d|$))
Demo
You can use this with preg_match_all. Each match will contain the number in the first group and the value in the second group.
Something like this should do the job:
preg_match_all('#\b(\d+)\s+(.+?(?=\s+\d|$))#', $input, $matches, PREG_SET_ORDER);
// $matches[0][1] == "130003"
// $matches[0][2] == "lebtro is cool"
// $matches[1][1] == "609664"
// $matches[2][2] == "trochanter2"
With this solution, any whitespace-separated number is considered as an entry start. If all your numbers are 6 digits, then replace \d+ with \d{6} (or \d{6,} for at least 6 digits).
with preg_split:
$result = preg_split('~\s*\b(?=[0-9])~', $str, -1, PREG_SPLIT_NO_EMPTY);
or more waterproof:
$result = preg_split('~(?:\s|\A)\s*(?=[0-9])~', $str, -1, PREG_SPLIT_NO_EMPTY);
or a why not pattern:
$result = preg_split('~(?:\s+|\A\s*)(*SKIP)(?=[0-9])~', $str, -1, PREG_SPLIT_NO_EMPTY);
Without regex:
$elts = explode(' ', $str);
$result = array();
$tmp = '';
foreach ($elts as $elt) {
if (ctype_digit($elt)) {
if ($tmp) $result[] = $tmp;
$tmp = $elt;
} else
$tmp .= ' ' . $elt;
}
$result[] = $tmp;
Related
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 1 year ago.
Improve this question
I would like to replace a user input with my own string, with the user's inputted message only having a known start character.
The user enters their input "#userinput" which will then change to
userinput
Any help would be greatly appreciated, my attempts can be seen below.
My attempts.
Code:
preg_match('#(?>\#)(\w*)#', $string, $matches);
foreach($matches as $row){
$string = preg_replace('#(?>\#)(\w*)#', ' '. $row . "", $string);
}
OTHER ATTEMPT
$string = preg_replace('#\#(.*)#', '<b><a href="=$1"=> #$1 </a></b>', $string);
You probably want to exclude space and #:
$string = preg_replace('##([^\s#]+)#', '<b> #$1 </b>', $string);
Try this
$atPosition = stripos($string, '#');
$substr = '';
if (stripos($string, '#') !== false) {
$substr = substr($string, $atPosition + 1);
$content = '<b> '.$substr.' </b>';
}
echo $content;
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
[i][b][i][b](This is a paragraph with BBcode.)[/b][/i][/b][/i]
Some of my BB code has double tags, whats the best way to remove this?
I've tried a few things mostly regex, but I'm honestly a novice when it comes to regex.
This is absolutely horrible, but it works.
<?php
$bb = '[i][b][i][b](This is a paragraph with BBcode.)[/b][/i][/b][/i]';
// regex with start, paragraph, and end capture groups
$regex = '#(?<start>(\[[a-z]*\])*+)(?<paragraph>.*)(?<end>(\[\/[a-z]*\])*+)#U';
// put matches into $matches array
preg_match_all($regex, $bb, $matches);
// get the stuff we need
$start = $matches['start'][0]; // string(12) "[i][b][i][b]"
$paragraph = implode('', $matches['paragraph']);
// now we will grab each tag
$regex = '#\[(?<tag>[a-z])\]#';
preg_match_all($regex, $start, $matches);
$tags = array_unique($matches['tag']);
// and build up the new string
$newString = '';
foreach($tags as $tag) {
$newString .= '[' . $tag . ']';
}
// create the end tags
$end = str_replace('[', '[/', $newString);
// put it all together
$newString .= $paragraph . $end;
echo $newString; // [i][b](This is a paragraph with BBcode.)[/i][/b]
Which gives you [i][b](This is a paragraph with BBcode.)[/i][/b]
Check it here https://3v4l.org/O8UHO
You can try to extract all the tags with a regexp (maybe like /\[.*\]/U) and then iterate throught them, removing all the duplicates
Quick example :
$bb = '[i][b][i][b](This is a paragraph with BBcode.)[/b][/i][/b][/i]';
preg_match_all('/(\[.*\])/gU', $bb, $tags);
$metTags = [];
foreach($tags[0] as $tag) {
// tag has not been met, save it
if (in_array($tag, $metTags) === false) {
$metTags[] = $tag;
// tag have been met already
} else {
// remove it ONCE
$bb = preg_replace('#'.preg_quote($tag).'#', '', $bb, 1);
}
}
echo $bb;
That is probably not the best solution due to preg_replace() use, but still doing the job great.
Edit : add code
So i wrote a long winded method in php only. It basically loops through all the characters, then i when i find an "[" i check the rest for accepted tags. I end up with an array of all the styles and just text allowing me to remove the duplicate styles. Would show code but dont want to get laughed at :D
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
How to last word from our Variable for example
$name = "Salman Khan";
And I want ans in only because i want tp remove n in my string
$words = explode(' ',$name); // Break words into array
$noofwords = count($words); // Find out how many
unset($words[$noofwords-1]); // remove the last one (-1 because of zero-index)
$newstring = implode(' ',$words); //put back together
$newstring = substr($name, 0, strlen($name)-1);
Do you mean remove the last letter?
echo substr($name, 0, -1);
Or the last word?
echo explode(' ', $name)[0];
echo rtrim($name,"n");
OR
echo substr($name,0,-1);
OR
echo substr($name,0,strlen($name)-1);
Output
salman kha
Try out this one.
$name = 'salman khan';
$length = strlen($name);
$char = $name[$length-1];
echo rtrim($name,$char);
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 have a UTF8 string that contains letters and digits. For example:
"Hello World 37. What? 24 last 6650"
and I want to reverse only the digits but keep the numbers in the same place.
The output should be:
"Hello World 73. What? 42 last 0566"
echo preg_replace_callback('/\d+/', function (array $m) { return strrev($m[0]); }, $string);
Before I posted the question, I thought about it and got an idea that works for me, so I'm only posting this question to enrich the database.
function reverseNumbersInString($str){
$tokens = explode(" ", $str);
$res = "";
for ($i = 0; $i < sizeof($tokens); $i++){
if (intval($tokens[$i] > 0 )){
$tokens[$i] = strrev($tokens[$i]);
}
$res .= " " . $tokens[$i];
}
return $res;
}
Maybe something like this:
$string = "Hello World 37. What? 24 last 6650";
preg_match_all('/\d+/', $string, $matches, PREG_OFFSET_CAPTURE);
foreach ($matches[0] as $numberData) {
$numberArray = str_split($numberData[0]);
$reversedNumber = implode('', array_reverse($numberArray));
$string = substr_replace($string, $reversedNumber, $numberData[1], strlen($numberData[0]));
}
This should do it:
$stringWithReversedNumbers = preg_replace_callback(
'/\d+/',
function ($matches) {
return strrev($matches[0]);
},
$originalString
);
Alternatively, if you ONLY want to operate on numbers that are distinct words (i.e. not part of another word, as in if you want hello123goodbye to remain unmodified because the 123 isn't a word by itself), change \d+ to \b\d+\b
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
In PHP given this string:
$string = '/sometext?123#abc/moretext';
How can I test for the existence of the pattern "?123#abc/" which will always be enclosed by "?" and "/" but have varying inner-text that may include any text and symbols? The text outside of the pattern will also be different. I need to do this:
if ($string includes pattern ?*/) {
//load the inner value into a variable
//then remove the entire patern including the leading "?" and trailing "/" and replace with a single "/"
}
How do I do this?
<?php
$string = '/sometext?123#abc/moretext';
$pattern = '/\\?(.*?)\\//';
if( $pieces = preg_split($pattern, $string, Null, PREG_SPLIT_DELIM_CAPTURE)) {
echo($pieces[1] . "\n");
unset($pieces[1]);
echo(implode("/", $pieces) . "\n");
}
?>
--output:--
~/php_programs$ php 1.php
123#abc
/sometext/moretext
Try this
$s = '/sometext?123#abc/moretext';
$matches = array();
$t = preg_match('#\?(.*?)\/#s', $s, $matches);
if($matches[1])
echo "match";
else
echo "not";
Output
match
Codepad