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 have an array of keys and a medium/long string.
I need to replace only max 2 keys that I found in this text with the same keys wrapped with a link.
Thanks.
ex.:
$aKeys = array();
$aKeys[] = "beautiful";
$aKeys[] = "text";
$aKeys[] = "awesome";
...
$aLink = array();
$aLink[] = "http://www.domain1.com";
$aLink[] = "http://www.domain2.com";
$myText = "This is my beautiful awesome text";
should became "This is my <a href='http://www.domain1.com'>beautiful</a> awesome <a href='http://www.domain2.com'>text</a>";
Don't really understood what you need but you can do something like:
$aText = explode(" ", $myText);
$iUsedDomain = 0;
foreach($aText as $sWord){
if(in_array($sWord, $aKeys) and $iUsedDomain < 2){
echo "<a href='".$aLink[$iUsedDomain++]."'>".$sWord."</a> ";
}
else{ echo $sWord." "; }
}
So, you could use a snippet like this. I recommend you to update this code by using clean classes instead of stuff like global - just used this to show you how you could solve this with less code.
// 2 is the number of allowed replacements
echo preg_replace_callback('!('.implode('|', $aKeys).')!', 'yourCallbackFunction', $myText, 2);
function yourCallbackFunction ($matches)
{
// Get the link array defined outside of this function (NOT recommended)
global $aLink;
// Buffer the url
$url = $aLink[0];
// Do this to reset the indexes of your aray
unset($aLink[0]);
$aLink = array_merge($aLink);
// Do the replace
return ''.$matches[1].'';
}
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 8 years ago.
Improve this question
I have some form in a variable. I have some input fields with names. How can I get all name="" from that string?
Now I'm trying this:
preg_match_all ('/(name="(.*?"))\d/s', $ki, $matches1);
But not working well. It gives me just a few and it gives me a lots of unnececary informations.
Try with this regex:
preg_match_all( '/name\s*=\s*(?|"(.*?)"|\'(.*?)\')/', $ki, $matches1 );
You can also do this using array.
i.e. By using explode method
$content='name="name1" other example text name="name2" other example text name="name3" ';
$finalResult=array();
function getBetweenAll($content,$start,$end)
{
$res = explode($start , $content , 2);
if(isset($res[1]) && $res[1]!='' )
{
$value = explode($end , $res[1] , 2);
global $finalResult;
$finalResult[]=$value[0];
if(isset($value[1]) && $value[1]!='')
{
$content=$value[1];
getBetweenAll($content,$start,$end);
}
}
}
$start='name="';
$end='"';
getBetweenAll($content,$start,$end);
echo "<pre>";
print_r($finalResult);
echo "</pre>";
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
Well I am trying to make a simple php system.
Anywise I need to separate the text when I want to add it to the database.
So for example I want to add:
abc:123
I want that the : will be the separater, so it'll look like this:
abc
123
And then both will go to a different table.
Could someone help me with this? As I am not an experience PHP coder, yet I am willing to learn how to do this.
Kind regards
This is pretty basic stuff..
$data = explode(':','abc:123');
foreach($data as $word)
{
// some code here
}
Use Split:
<?php
$data = "abc:123";
list ($var1, $var2) = split (':', $data);
echo "Var1: $var1; Var2: $var2;<br />\n";
?>
You can achieve this using explode.
abc:123
Is a string. Let's define it as a variable:
$origin = "abc:123";
You can split the string, using : as the separator.
$separator = ":";
$exploded = explode($separator, $origin);
Now you have an array which you can use to access abc and 123 individually.
$pre = $exploded[0];
$post = $exploded[1];
You don't know how many splits there will be?
That's okay. Your array simply increases, meaning you can simply loop through the array and handle the values.
foreach ($exploded as $split)
{
// Do something with $split
}
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
Is it possible to get the text between <p></p> tags and set this in a variable?
<p>blabla</p> So i would like to get the text "blabla" and set this into a php variable so the variable would have the text value like this:.
<?$test = blabla;?>
Try:
$html = "<p>blabla</p>";
$dom = new DOMDocument;
$dom->loadXML($html);
$arr = $dom->getElementsByTagName('p');
foreach ($arr as $value) {
echo $value->nodeValue; // result => blabla
}
There are many methods which can be used based on your needs so take a look on documentation
DOMDocument
You can use this function, it is self explanatory:
function getTextBetweenTags($string, $tagname)
{
$pattern = "/<$tagname>(.*?)<\/$tagname>/";
preg_match($pattern, $string, $matches);
return $matches[1];
}
?>
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 9 years ago.
Improve this question
I created a function in PHP first and then following it tried to make the same thing in Python. Following are my codes
Python:
def bfs(my_data):
my_queue = []
my_queue.insert(0, my_data[0]);
my_data[0]['visited'] = '1';
while my_queue:
vertex = my_queue.pop()
print(vertex['letter'])
for n_vertex in vertex['neighbors']:
int_vertex = int(n_vertex)-1
if my_data[int_vertex]['visited'] is '0':
my_data[int_vertex]['visited'] = '1'
test.insert(0, my_data[int_vertex])
my_queue = str(test)
PHP:
function bfs($my_data)
{
$my_queue = array(); //array to store vertices
array_unshift($my_queue, $my_data[0]); // pass the first value to the first index of queue
$my_data[0]['visited'] = true; // value for visited is set to true for the first vertix
//print_r($my_queue);
while(!empty($my_queue))
{
$vertex = array_pop($my_queue); // passing the last value of queue to vertex
echo $vertex['letter'];
$msg = $vertex['letter'];
$output_file = $_POST["output_file_name"];
$output_file_path = "../SPA/" . $output_file;
$outfile = fopen($output_file_path, 'aw'); //writing output to the file
fwrite($outfile, $msg);
fclose($outfile);
// fwrite($outfile, $msg);
foreach($vertex['neighbours'] as $n_vertex)
{
//print_r($n_vertex);
if(!$my_data[$n_vertex-1]['visited'])
{
$my_data[$n_vertex-1]['visited'] = true; // set visited true after visiting each neighbour
array_unshift($my_queue, $my_data[$n_vertex-1]); //pass the neighbours to queue
}
}
}
}
I believe both are same functions but as i am getting different results i am trying to find out the difference. What do you think? Also, if they are different can you tell me how?
This is quite a broad question (what results are you seeing, what do you suspect?) But, for one thing, the for loop in Python is not indented to be within the while, whereas in PHP it is inside the while loop.
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
must create a rule that captures the first preg_replace bar url:
http://localhost/item/other
must take the rule just 'item' regardless of what comes after
You should use the php parse_url() function
An example would be:
$parts = parse_url("http://localhost/item/other");
$path_parts= explode('/', $parts[path]);
$item = $path_parts[1];
echo $item;
It does not look like you have a specific question. I am assuming you are about write some routes script.
$request_uri = explode('/', $_SERVER['REQUEST_URI']);
$delimiter = array_shift($request_uri);
$controller = array_shift($request_uri);
$action = array_shift($request_uri);
if(preg_match('/item/i', $controller))
{
// do something right
}
else
{
// do something wrong
}