Existing code I have draws a txt file and first converts http elements to links and then converts hashtag elements to links then prints the result. At the end of every text is a dash then a time and date (DOY and YEAR with leading zero - for a reason). The text echos on the page as (ex.)
Blah blah blah and blah blah - 3:47:32 310 017
or time/date variations of
14:09:47 23 017
7:38:83 9 017
so there is no set figure of characters
$text = file_get_contents("temp.txt");
$link = preg_replace('#(https?://([-\w\.]+)+(:\d+)?(/([-\w/_\.]*(\?\S+)?)?)?)#', '$1', $text);
$hash = preg_replace('/(?<!\S)#([0-9a-zA-Z]+)/m', '#$1', $link);
echo $hash;
As an admitted novice, I have not been able to best translate the entirety of the syntax that creates the above preg_replace(s) to understand it well enough to make use of it to apply to wanting to do the same with the end time/date. I have made several attempts but have seen no results to demonstrate I am even going in the proper direction.
My thought process is that the order is
0?colon00colon00space0??space000
are the identities of the positions to seek.
You could use this regular expression:
$link = preg_replace('#\b(\d\d?:\d\d:\d\d [1-9]\d{0,2} 0\d\d)\b#', '$1', $text);
Related
I have the following:
$string = '4745518 some text 4510018 some text 4743618 4745518 some text 4510518 some text';
$newstring = preg_replace('/[1-9]{7,7}/','NEWTRANSACTION: $0',$string);
My intent is "replace all occurrences of seven digits with 'NEWTRANSACTION: ' plus those seven digits."
However, my result is:
NEWTRANS: 4745518 some text 4510018 some text NEWTRANS: 4743618 NEWTRANS: 4745518
some text 4510518 some text
In other words, it appears that only some of the seven-digit groups are being replaced. If I edit the original string, shift the seven digit groups around, those same seven digit groups get replaced. It's like only certain combinations of numbers are being marked for replacement. My actual input string is hundreds of lines long, and it really appears that random seven-digit groups are being replaced.
Can anyone see what's wrong? Thanks in advance.
=== EDIT ===
Thanks for all of the help so quickly. I would up using
/\b\d{7}\b/
and it works like a charm. I'm new to regex, so I learned a bit here -- although not realizing the missing '0' was total boneheadedness on my part.
My bad, showing 'NEWTRANSACTION: ' in the code, but showing 'NEWTRAN:' in the output. I was just typing the output, instead of copy/paste, and shortened it accidentally.
Thanks again.
Your code working fie after changing [1-9] to [0-9] (As your digits have 0 also at some places)
<?php
$string = '4745518 some text 4510018 some text 4743618 4745518 some text 4510518 some text';
echo $newstring = preg_replace('/[0-9]{7,7}/','NEWTRANSACTION: $0',$string);
https://eval.in/984686
Note:- A much shorter code given in a comment by #GrumpyCrouton,#kaii and #Barmar
/\b\d{7}\b/
Output:-https://eval.in/984792
I am using regular expressions with preg_replace() in order to find and replace a sentence in a piece of text. The $search_string contains plain text + html tags + elements. The problem is that only sometimes the elements convert to white space on run time, making it difficult to find and replace using str_replace(). So, I'm trying to build a pattern that is equal to the search string and will match anything like it which contains, or does not contain the elements;
For example:
$search_string = 'Two years in, the company has expanded to 35 cities, five of which are outside the U.S. Plus, in April, ClassPass acquired its main competitor, Fitmob.';
$pattern = $search_string(BUT IGNORE THE elements in the subject)
$subject = "text text text text text". $search_string . "text text text text text";
Using A regular expression to exclude a word/string, I've tried:
$pattern = '`^/(?!\ )'.$search_string.'`';
$output = preg_replace($pattern, $replacement_string,$subject);
The end result will be that if the $subject does contains a string that is like my $seach_string but without the elements, it will still match and replace it with $replacement_string
EDIT:
The actual values:
$subject = file_get_contents("http://venturebeat.com/2015/11/10/sources-classpass-raises-30-million-from-google-ventures-and-others/");
$search_string = "Two years in, the company has expanded to 35 cities, five of which are outside the U.S. Plus, in April, ClassPass acquired its main competitor, Fitmob.";
$replacement_string = "<span class='smth'>Two years in, the company has expanded to 35 cities, five of which are outside the U.S. Plus, in April, ClassPass acquired its main competitor, Fitmob.</span>";
Not a very efficient way of doing it but it should workout for you,
preg_replace('Two.*?years.*?in.*?the.*?company.*?has.*?expanded.*?to.*?35.*?cities.*?five.*?of.*?which.*?are.*?outside.*?the.*?U\.S\..*?Plus.*?in.*?April.*?ClassPass.*?acquired.*?its.*?main.*?competitor.*?Fitmob\.', '<span class=\'smth\'>$0</span>', $subject);
I am building a system to create dynamic descriptions for meta tags. It takes the post on the page and feeds it into a function which stripes out everything unnecessary and then takes the strlen see that its to large and creates a list of words. Now, I need to remove the right amount of words to bring the string down to 155 characters or 152 and I will add an ellipsis.
Example String (None of this is actual code its meant for sudo code)
$string = "Hello lovely Solia Avatar Community, I have a little problem and I need your help. I used to have Paint Tool SAI but my laptop ate a lot of my files, one of them being SAI. Now I am trying to get it back but I lost the website I got it from. I keep finding a website to buy it from for about $70.";
echo strlen($string); = 296
if(strlen($string) > 155) {
// Get word amount
$words = preg_split('/\s+/', ltrim($string), 155 + 1);
}
Now, I have the words in an array and I need to take that array and bring it down to a total strlen of 155 and stop at the nearest word and not break it awkwardly. Maybe I am going about trying to solve this problem incorrectly and I need to be using a different set of functions.
The basic idea is to find the position of the first space after the first 155 characters. This can be done with strpos($string, ' ', 155). Then use substr($string, 0, $endat155) to retrieve the portion of the string from the beginning to that position.
$endat155 = strpos($string, ' ', 155);
$firstWords = substr($string, 0, $endat155);
echo $firstWords;
So I've been working on a little project to write a syntax highlighter for a game's scripting language. It's all gone off without a hitch, except for one part: the numbers.
Take these lines for example
(5:42) Set database entry {healthpoints2} to the value 100.
(5:140) Move the user to position (29,40) on the map.
I want to highlight that 100 on the end, without highlighting the (5:42) or the 2 in the braces. The numbers won't always be in the same place, and there won't always only be one number.
I basically need a regexp to say:
"Match any numbers that aren't anywhere between {} and don't match the (#:#) pattern."
I've been at this for a day and a half now and I'm pulling out my hair trying to figure it out. Help with this would be greatly appreciated!
I've already looked at regular-expressions.info, and tried playing around with RegexBuddy, but i'm just not getting it :c
Edit: By request, here's some more lines copied right from the script editor.
(0:7) When somebody moves into position (**10** fhejwkfhwjekf **20**,
(0:20) When somebody rolls exactly **10** on **2** dice of **6** sides,
(0:31) When somebody says {...},
(3:3) within the diamond (**5**,**10**) - **20** //// **25**,
(3:14) in a line starting at (#, #) and going # more spaces northeast.
(5:10) play sound # to everyone who can see (#,#).
(5:14) move the user to (#,#) if there's nobody already there.
(5:272) set message ~msg to be the portion of message ~msg from position # to position #.
(5:302) take variable %var and add # to it.
(5:600) set database entry {...} about the user to #.
(5:601) set database entry {...} about the user named {...} to #.
You might kick yourself when you see this solution...
Assuming this desired number will always be used in a sentence, it should always have a space preceding it.
$pattern = '/ [0-9]+/s';
If the preceding space isn't always present, let me know and I'll update the answer.
Here's the updated regex to match the 2 examples in your question:
$pattern = '/[^:{}0-9]([0-9,]+)[^:{}0-9]/s';
3nd update to account for your question revisions:
$pattern = '/[^:{}0-9a-z#]([0-9]+[, ]?[0-9]*)[^:{}0-9a-z#]/s';
So you don't highlight the number in things like
{update 29 testing}
you might want to pre strip the braces, like so:
$pattern = '/[^:{}0-9a-z#]([0-9]+[, ]?[0-9]*)[^:{}0-9a-z#]/s';
$str = '(0:7) Hello {update 29 testing} 123 Rodger alpha charlie 99';
$tmp_str = preg_replace('/{[^}]+}/s', '', $str);
preg_match($pattern, $tmp_str, $matches);
(\d+,\s?\d+)|(?<![\(\{:]|:\d)\d+(?![\)\}])
http://regexr.com?30omd
Would this work?
I'm a regex newbie, please help me out. The string below occurs in one document:
not_unique\">20,000 miles under sea
I need to extract the number. The sequence "not_unique" is not unique and may occur in the whole document several times before this sample comes. The part "miles under sea" is unique for the document, can be used as ending delimiter.
I tried something like this in PHP, but it didn't work for me:
if (preg_match('/(?=.*?miles under sea)(?!.+?not_unique)not_unique/', $document, $regs)) {...}
Please help!
How about something like this?
<?php
$document = "blah blah blah sjhsdijf not_unique\">20,000 miles under sea</a> jkdjksds sdsjdlksdsd k skdjsld sd";
//the made optional, also account for 'leagues' instead of miles
preg_match("/([0-9,]{1,6})\s?(miles|leagues)\sunder(\sthe)?\ssea/i", $document, $matches);
print_r($matches);
?>
/ not unique\">\s*([0123456789,]+)\s*miles under the sea /
should do it.
This should do the trick:
preg_match_all('/[1234567890\,]+ miles under sea/i', 'not_unique\">20,000 miles under sea', $result); //find all occurances of the pattern
$tempval=$result[sizeof($result)-1]; //get the last one
$endresult=substr($tempval,0,strlen($tempval)-16); //get the string without the length of the ending string
If needed - replace 16 with the exact length of the ending string.