str_replace() 替换已经替换的文本

问题描述 投票:0回答:1

我可以阻止

str_replace()
影响已替换的文本吗?我正在尝试将匹配的单词转换为链接以在悬停时显示定义。一切都很好,只是我认为定义中的文本也被替换了。

正文:

Chance to be Chancellor enables you to learn about, and share your opinions on, the Budget and the Youth Budget is the result!

我们收集了所有参加“机会成为总理”活动的人的预算选择,并将其发布在青年预算中,以便让你们——未来的纳税人和选民——对 2013 年预算发表意见。

不再有效的内容链接:

http://www.payingforit.org.uk/chance-to-be-chancellor/youth-budget-2012/46037/

代码

ob_start();
the_content();
$content = ob_get_clean();  
$new_content = str_ireplace($match_term,$match_term_replacement,$content);

我的数组是:

$matches = Array (
    [0] =>  Youth Budget 
    [1] =>  Welfare system  
    [2] =>  Tax 
    [3] =>  Recession 
    [4] =>  Public spending   
    [5] =>  Public protection 
    [6] =>  PSHEe 
    [7] =>  Pfeg Quality Mark  
    [8] =>  Pension 
    [9] =>  National debt  
    [10] =>  Key Stage 4 
    [11] =>  Income Tax 
    [12] =>  HM Treasury 
    [13] =>  Global financial crisis 
    [14] =>  Gilts 
    [15] =>  Economic awareness 
    [16] =>  Deficit 
    [17] =>  Credit crunch 
    [18] =>  Chancellor 
    [19] =>  Citizenship  
    [20] =>  Budget 
)
   

替换为:

Array (
    [0] =>  <a class="matched_term" href="/about/glossary/#youth-budget"><span class="term">Youth Budget</span><span class="definition">A report from the Citizenship Foundation on the results of all those who take part in Chance to be Chancellor and share their opinions on the Budget of that year.</span></a> 
    [1] =>  <a class="matched_term" href="/about/glossary/#welfare-system"><span class="term">Welfare system </span><span class="definition">A social system where by the Government provides assistance to those individuals and families in need of assistance.</span></a> 
    [2] =>  <a class="matched_term" href="/about/glossary/#tax"><span class="term">Tax</span><span class="definition">A compulsory money contribution to the Government and taken from the producing and selling of goods and services, the earning of income and ownership of property etc.</span></a> 
    [3] =>  <a class="matched_term" href="/about/glossary/#recession"><span class="term">Recession</span><span class="definition">Period of general economic decline defined usually as a lack of economic growth for six months or longer and marked by high unemployment and a drop in retail sales.</span></a> 
    [4] =>  <a class="matched_term" href="/about/glossary/#public-spending"><span class="term">Public spending  </span><span class="definition">Money spent by a government to pay for public services and public infrastructure which is supported by taxation.</span></a> 
    [5] =>  <a class="matched_term" href="/about/glossary/#public-protection"><span class="term">Public protection</span><span class="definition">Is the part of the UK Budget which funds the police, courts and prison system along with emergency services.</span></a> 
    [6] =>  <a class="matched_term" href="/about/glossary/#pshee"><span class="term">PSHEe</span><span class="definition">Personal, social, health and economic education (PSHEe) is a planned programme of learning opportunities and experiences to equip young people with the knowledge, understanding, attitudes and practical skills to live healthily, safely, productively and responsibly.</span></a> 
    [7] =>  <a class="matched_term" href="/about/glossary/#pfeg-quality-mark"><span class="term">Pfeg Quality Mark </span><span class="definition">An accreditation system on financial and economic educational resources from pfeg (Personal Finance Education Group) – an independent charity providing knowledge, resources and support for teaching children about money.</span></a> 
    [8] =>  <a class="matched_term" href="/about/glossary/#pension"><span class="term">Pension</span><span class="definition">A pension is a benefit received by people after they have retired. There are three main types: personal where individuals pay regular amounts to a pension provider, company which are set up by employers and state paid by the Government.</span></a> 
    [9] =>  <a class="matched_term" href="/about/glossary/#national-debt"><span class="term">National debt </span><span class="definition">The sum of all previously incurred annual deficits. Since deficits are financed by government borrowing, national debt is equal to all outstanding government debt.</span></a> 
    [10] =>  <a class="matched_term" href="/about/glossary/#key-stage-4"><span class="term">Key Stage 4</span><span class="definition">The National Curriculum is organised into four 'key stages’. Key Stage 4 is aimed towards pupils in Years 10 and 11 (S3 and S4 in Scotland) usually between the ages of 14 and 16 years old.</span></a> 
    [11] =>  <a class="matched_term" href="/about/glossary/#income-tax"><span class="term">Income Tax</span><span class="definition">A tax paid on income earned by individuals working in the UK.</span></a> 
    [12] =>  <a class="matched_term" href="/about/glossary/#hm-treasury"><span class="term">HM Treasury</span><span class="definition">The UK Government department responsible for public finances and the country’s economic policy.</span></a> 
    [13] =>  <a class="matched_term" href="/about/glossary/#global-financial-crisis"><span class="term">Global financial crisis</span><span class="definition">A situation where the value of financial institutions (e.g. banks) and/or financial products (e.g. mortgages) drops rapidly due to a collapse in confidence in them.</span></a> 
    [14] =>  <a class="matched_term" href="/about/glossary/#gilts"><span class="term">Gilts</span><span class="definition">An investment opportunity issued by the UK Government to borrow money from investors for a defined period of time at a fixed interest rate in order to finance public spending.</span></a> 
    [15] =>  <a class="matched_term" href="/about/glossary/#economic-awareness"><span class="term">Economic awareness</span><span class="definition">Describes individuals’ knowledge and understanding of the economy, particularly how individuals, business and governments shape it and how the economy shapes society.</span></a> 
    [16] =>  <a class="matched_term" href="/about/glossary/#deficit"><span class="term">Deficit</span><span class="definition">A situation in which government spending exceeds the income it is able to collect from taxation.</span></a> 
    [17] =>  <a class="matched_term" href="/about/glossary/#credit-crunch"><span class="term">Credit crunch</span><span class="definition">An economic condition when credit becomes more difficult to obtain due to banks and financial companies becoming more wary of lending money to businesses and individuals.</span></a> 
    [18] =>  <a class="matched_term" href="/about/glossary/#chancellor"><span class="term">Chancellor</span><span class="definition">British Government minister in charge of the HM Treasury department and responsible for economic and financial matters.</span></a> 
    [19] =>  <a class="matched_term" href="/about/glossary/#citizenship"><span class="term">Citizenship </span><span class="definition">A statutory subject in the National Curriculum taught to all pupils aged 11–16 years old in maintained schools in England. Citizenship explores concepts of democracy, justice, rights and responsibilities, identities and diversity.</span></a> 
    [20] =>  <a class="matched_term" href="/about/glossary/#budget"><span class="term">Budget</span><span class="definition">A statement made every year by the government on how it’s going to get the money it needs to spend on the country.</span></a> 
)
php wordpress replace
1个回答
0
投票

好的 - 所以首先将其替换为唯一 ID,然后再次替换。

$args = array('post_type'=>'glossary-term','posts_per_page'=>-1);

$terms = get_posts($args);

ob_start();
the_content();
$content = ob_get_clean();  

foreach ($terms as $term) {
    $match_id[]=' gloss_id_'.$term->ID.' ';
    $match_term[]=' '.$term->post_title.' ';
    $match_term_replacement[] = ' <a class="matched_term" href="/about/glossary/#'.$term->post_name.'"><span class="term">'.$term->post_title.'</span><span class="definition">'.$term->post_content.'</span></a>'.' ';
    $match_term_id_replacement[] = ' gloss_id_'.$term->ID.' ';
}       

$new_content = str_ireplace($match_term,$match_term_id_replacement,$content);
$new_contentb = str_ireplace($match_id,$match_term_replacement,$new_content);

echo $new_contentb;
© www.soinside.com 2019 - 2024. All rights reserved.