So I am using a preg_match to get any text after a # up until a space out of a string. However if there are multiple occasions of it in the string it will only return the first one. This is what I have so far
$text = '#demo1 #demo2 some text #blah2';
$check_hash = preg_match("/([#][a-zA-Z-0-9]+)/", $text, $hashtweet);
foreach ($hashtweet as $ht){
echo $ht;
}
The echo $ht; outputs #demo1#demo1 when it should output all 3 of the words with # in front. Any help is greatly appreciated.
You want to use preg_match_all.
Example:
<?php
$text = '#demo1 #demo2 some text #blah2';
$check_hash = preg_match_all("/([#][a-zA-Z-0-9]+)/", $text, $hashtweet);
foreach ($hashtweet[1] as $ht){
echo $ht;
}
Check preg_match_all
Related
I want to get the words in a string where a specific character stands before, in this case, its the : character.
textexjkladjladf :theword texttextext :otherword :anotherword
From this snippet the expected output will be:
theword
otherword
anotherword
How do i do this with PHP?
You can use regular expression:
$string = "textexjkladjladf :theword texttextext :otherword :anotherword";
$matches = array();
preg_match_all('/(?<=:)\w+/', $string, $matches);
foreach ($matches[0] as $item) {
echo $item."<br />";
}
Output is:
theword
otherword
anotherword
The array what you want is the $matches[0]
Another way of getting those words without Regular Expression can be:
Use explode(' ',$str) to get all the words.
Then loop the words and check which one starts with ':'.
try
$str = 'textexjkladjladf :theword texttextext :otherword :anotherword';
$tab = exlode(':',$str);
print_r($tab);
//if echo entry 0 =>
echo $tab[0]; // => textexjkladjladf
So i've been trying to get this bit of code to work all day and haven't been able to do it... I wnat to be able to replace letters with a number (or just a value) from an array. this is the code i've got:
$l2n =
array(
'a'=>'1',
'b'=>'2',
'c'=>'3',
'd'=>'4',
'e'=>'5',
'f'=>6,
'g'=>7,
'h'=>8,
'i'=>9,
'j'=>10,
'k'=>11,
'l'=>12,
'm'=>13,
'n'=>14,
'o'=>15,
'p'=>16,
'q'=>17,
'r'=>18,
's'=>19,
't'=>20,
'u'=>21,
'v'=>22,
'w'=>23,
'x'=>24,
'y'=>25,
'z'=>16
);
$string = str_split($string);
$explode = array_shift($string);
if($l2n[$explode] == $explode)
{
echo $l2n[$explode];
}
else
{
echo $l2n['a'];
}
I tried to use Preg_replace but i've never had a good expereince with that function. so If anybody could help me out, hint me in the correct direction, that'd be great.
You can just use str_replace once you've used array_keys and array_values to get each side of the array:
$keys = array_keys($l2n);
$values = array_values($l2n);
$yourstring = 'Hello world!';
echo str_replace($keys, $values, $yourstring);
// H5121215 231518124!
Demo: https://eval.in/77453
Docs:
http://php.net/str_replace
http://php.net/array_keys
http://php.net/array_values
You can simply do:
$string = preg_replace(array_keys($l2n), array_values($l2n), $string);
From the documentation:
If both pattern and replacement parameters are arrays, each pattern will be replaced by the replacement counterpart.
Why in the world would you use an array for this? Isn't ord() what you are looking for here?
$string = "ABCDE";
foreach ( str_split($string) as $chr ) {
echo ord($chr) - 64; // or 97 if they all are lowercase
echo PHP_EOL;
}
I have to extract the email from the following string:
$string = 'other_text_here to=<my.email#domain.fr> other_text_here <my.email#domain.fr> other_text_here';
The server send me logs and there i have this kind of format, how can i get the email into a variable without "to=<" and ">"?
Update: I've updated the question, seems like that email can be found many times in the string and the regular expresion won't work well with it.
You can try with a more restrictive Regex.
$string = 'other_text_here to=<my.email#domain.fr> other_text_here';
preg_match('/to=<([A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,4})>/i', $string, $matches);
echo $matches[1];
Simple regular expression should be able to do it:
$string = 'other_text_here to=<my.email#domain.fr> other_text_here';
preg_match( "/\<(.*)\>/", $string, $r );
$email = $r[1];
When you echo $email, you get "my.email#domain.fr"
Try this:
<?php
$str = "The day is <tag> beautiful </tag> isn't it? ";
preg_match("'<tag>(.*?)</tag>'si", $str, $match);
$output = array_pop($match);
echo $output;
?>
output:
beautiful
Regular expression would be easy if you are certain the < and > aren't used anywhere else in the string:
if (preg_match_all('/<(.*?)>/', $string, $emails)) {
array_shift($emails); // Take the first match (the whole string) off the array
}
// $emails is now an array of emails if any exist in the string
The parentheses tell it to capture for the $matches array. The .* picks up any characters and the ? tells it to not be greedy, so the > isn't picked up with it.
I'm trying to extract the following in php, but my regex or egreg is out of place and i got really confused. Please help me put these to two variables:
<a onmouseover="dgsa.sm(this)" onmouseout="dgsa.hm();" href="http://www.cnn.com/testpage.html#page_mostview">test titles</a>
I want the variable:
$url="http://www.cnn.com/testpage.html#page_mostview";
$title="test titles";
Any kind of help on this is greatly appreciated.
If you have in one big string:
<?php
$str = 'Hello Bye';
$expr = '/<a[^>]*? href=\"(?<url>[^\"]+)\"[^>]*?>(?<text>.*?)<\/a>/';
echo preg_match_all($expr, $str, $amatches);
echo '<br><br>';
print_r($amatches);
?>
You can also use:
preg_match_all($expr, $str, $amatches, PREG_SET_ORDER);
Which may suit better (I ofen prefer this approach)
If you have in spearate strings and handle in a loop, use preg_match instead. Tis will only return the first, and you loop.
Edit: here it is with your example, as requested.
<?php
$str = '<a onmouseover="dgsa.sm(this)" onmouseout="dgsa.hm();" href="http://www.cnn.com/testpage.html#page_mostview">test titles</a>';
$expr = '/<a[^>]*? href=\"(?<url>[^\"]+)\"[^>]*?>(?<text>.*?)<\/a>/';
echo preg_match_all($expr, $str, $amatches, PREG_OFFSET_CAPTURE);
echo '<br><br>';
print_r($amatches);
?>
how can i get 41P86246HOH7C1G4A983321910HDL63U9 from the following with preg_match
input type="text" value="41P86246HOH7C1G4A983321910HDL63U9" id=""
DOMDocument::loadHTML("<$input>")->getElementsByTagName('input')
->item(0)->getAttribute('value');
What about something like this :
$str = 'input type="text" value="41P86246HOH7C1G4A983321910HDL63U9" id=""';
$m = array();
if (preg_match('#value="([^"]+)"#', $str, $m)) {
var_dump($m[1]);
}
Which will match everything between the double quotes that come with value, and get you :
string '41P86246HOH7C1G4A983321910HDL63U9' (length=33)
But, as a sidenote : if you are trying to "parse" HTML with regex, it's generally not the "best" way ; HTML is not quite regular enough for regex...
Simply, without extra characters:
preg_match('/(?<=value=")[0-9A-Za-z]+/', $str, $match);
Your result is in $match[0];
With something like this:
if(preg_match('#value="([^"]*)"#', $text, $m)){
echo $m[1];
}
But you can also make something who split the string in each key with this value.
function attributes($text){
$attrs = array();
if(preg_match_all('#(\b[^=]*\b)\s*=\s*"([^"]+)"#', $text, $matches, PREG_SET_ORDER)){
foreach($matches as $m){
$attrs[$m[1]] = $m[2];
}
}
return $attrs;
}
// Use like this
$attrs = attributes('input value="bla"');
if(isset($attrs['value'])){
echo $attrs['value'];
}
don't even have to use regex. Just use PHP's string methods
$str='input type="text" value="41P86246HOH7C1G4A983321910HDL63U9" id=""';
$s = explode(" ",$str);
// go through each element, find "value"
foreach($s as $a=>$b){
if(strpos($b,"value")!==FALSE){
$find = explode("=",$b);
print $find[1];
}
}